如何在 WCF 中评估服务器上的自定义 ClientCredentials?

How can I evaluate custom ClientCredentials on the server in WCF?

我遇到了以下情况:

windows"fat client" 应用程序正在连接到 WCF 网络服务。客户端和网络服务都使用完全相同的绑定,如下所示:

private static NetTcpBinding Message_Security_UserName_Credentials()
    {
        NetTcpBinding binding = new NetTcpBinding();

        binding.Security.Mode = SecurityMode.Message; 

        binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;

        binding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;

        binding.PortSharingEnabled = true; 

        return binding;
    }

客户端将 "custom" 客户端凭据发送到网络服务。自定义客户端凭证 class 是这样的:

public class CustomClientCredentials : ClientCredentials
{

public CustomClientCredentials()
{
    AuthorizationToken = String.Empty;

    this.ClientCertificate.Certificate = Certificates.ClientPFX;

    this.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.Custom;

    this.ServiceCertificate.Authentication.CustomCertificateValidator = new CustomClientX509CertificateValidator("CN");
}

private string authorizationtoken;
public string AuthorizationToken
{
    get
    {
        return this.authorizationtoken;
    }
    set
    {
        if (value == null)
        {
            throw new ArgumentNullException("value");
        }
        this.authorizationtoken = value;
    }
}


public String Name
{
    set
    {
        this.UserName.UserName = value;
    }
}

public String Password
{
    set
    {
        this.UserName.Password = value;
    }
}

protected CustomClientCredentials(CustomClientCredentials other)
    : base(other)
{
    this.AuthorizationToken = other.AuthorizationToken;
}

protected override ClientCredentials CloneCore()
{
    return new CustomClientCredentials(this);
}

}

简而言之,将自定义客户端凭据发送到服务的过程如下所示:

ChannelFactory<ILoginService> factory = new ChannelFactory<ILoginService>   (binding, endpointaddress);

factory.Endpoint.Behaviors.Remove<ClientCredentials>();

CustomClientCredentials credentials = new CustomClientCredentials() {Name = this.User.EMail, Password = this.User.Password, AuthorizationToken = String.Empty};

factory.Endpoint.Behaviors.Add(credentials);

ILoginService client = factory.CreateChannel();

Token result = client.LogIn();

在服务器上,我使用自定义 UserPasswordValidator 来读取客户端凭据。它看起来像这样:

public class CustomServiceUserNamePasswordValidator :   System.IdentityModel.Selectors.UserNamePasswordValidator
{
    public override void Validate(string userName, string password)
    {
        if (null == userName || null == password)
        {
            throw new ArgumentNullException();
        }
    }
}

到目前为止一切正常。正如您在我的自定义 ClientCredentials class 中看到的那样,我想向服务器发送更多附加信息。

我的问题是:我必须做什么才能读出服务器上收到的自定义客户端凭据?

我的想法是,我必须简单地告诉服务器上的服务端点,他应该期待某种类型的凭据,然后他可以评估它们。

验证自定义客户端凭据可能不是一项简单的任务,但您可以按照此 link for validation. I would suggest also to follow this link 执行自定义凭据。