2017-06-16 11 views
0

独自のユーザー名/パスワードで認証される複数のエンドポイントを作成することはできますか? (各エンドポイントはそれ自身の資格情報を持っています)それぞれ独自のユーザー名/パスワードを持つ複数のエンドポイントWCF

私は1つのエンドポイントの例があり、うまく動作します。同じ認証方法で複数のエンドポイントを追加する方法はわかりません。

私の例:

String adress1 = "http://localhost/CalculatorService"; 
     String adress2 = "http://localhost/CalculatorService/en1/"; 
     Uri[] baseAddresses = { new Uri(adress1) }; 

     ServiceHost host = new ServiceHost(typeof(CalculatorService), baseAddresses); 
     ContractDescription contDesc = ContractDescription.GetContract(typeof(ICalculator)); 

     ServiceCredentials cd = new ServiceCredentials(); 
     cd.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom; 
     cd.UserNameAuthentication.CustomUserNamePasswordValidator = new CustomUserNameValidator(); 

     BasicHttpBinding b1 = new BasicHttpBinding(); 
     b1.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; 
     b1.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 

     ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
     smb.HttpGetEnabled = true; 
     smb.HttpGetUrl = new Uri(adress1); 

     host.Description.Behaviors.Add(cd); 
     host.Description.Behaviors.Add(smb); 

     EndpointAddress adr1 = new EndpointAddress(baseAddresses[0]); 

     ServiceEndpoint en1 = new ServiceEndpoint(contDesc); 
     en1.Binding = b1; 
     en1.Address = adr1; 
     en1.Name = "en1"; 


     ServiceEndpoint en2 = new ServiceEndpoint(contDesc); 
     en2.Binding = new BasicHttpBinding(); 
     en2.Address = new EndpointAddress(adress2); 
     en2.Name = "en2"; 

     host.AddServiceEndpoint(en1); 
     host.AddServiceEndpoint(en2); 

     host.Open(); 

認証クラス:

class CustomUserNameValidator : UserNamePasswordValidator 
    { 
    public override void Validate(string userName, string password) 
    { 
     if (userName.ToLower() != "test" || password.ToLower() != "test") 
     { 
     throw new SecurityTokenException("Unknown Username or Incorrect Password"); 
     } 
    } 
    } 

インタフェース/クラス:

[ServiceContract] 
    public interface ICalculator 
    { 
    [OperationContract] 
    double Add(double n1, double n2); 
    [OperationContract] 
    double Subtract(double n1, double n2); 
    [OperationContract] 
    double Multiply(double n1, double n2); 
    [OperationContract] 
    double Divide(double n1, double n2); 
    } 


    public class CalculatorService : ICalculator 
    { 
    public double Add(double n1, double n2) 
    { 
     return n1 + n2; 
    } 
    public double Subtract(double n1, double n2) 
    { 
     return n1 - n2; 
    } 
    public double Multiply(double n1, double n2) 
    { 
     return n1 * n2; 
    } 
    public double Divide(double n1, double n2) 
    { 
     return n1/n2; 
    } 
    } 

答えて

1

これはthis questionに似ています。特定のエンドポイントではなく、サービスへのアクセスを認証するため、私の知るところによると、これはWCFでは不可能です。

関連する問題