【问题标题】:How can I evaluate custom ClientCredentials on the server in WCF?如何在 WCF 中评估服务器上的自定义 ClientCredentials?
【发布时间】:2015-08-09 04:31:57
【问题描述】:

我有以下情况:

Windows“胖客户端”应用程序正在连接到 WCF Web 服务。客户端和 Web 服务都使用完全相同的绑定,如下所示:

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;
    }

客户端将“自定义”客户端凭据发送到 Web 服务。自定义客户端凭据类是这样的:

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 类中看到的那样,我想向服务器发送更多附加信息。

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

我脑海中的理论是,我必须告诉服务器上的服务端点,他应该期望某种类型的凭据,然后他可以评估它们。

【问题讨论】:

    标签: c# web-services wcf credentials nettcpbinding


    【解决方案1】:

    验证自定义客户端凭据可能不是一件容易的事,但您可以按照此link 进行验证。我还建议遵循此link 进行自定义凭据实施。

    【讨论】:

    • 我遵循了这两个链接,并且完全按照说明进行操作,并且效果很好。每个人都应该知道的,因为它可能有点令人困惑:对于像我这样以编程方式完成所有 WCF 工作的人来说,可以跳过“应用程序配置”步骤。您只需要这些类:ClientCredentials、SecurityTokenManager、SecurityTokenAuthenticator 和 IAuthorizationPolicy。这在双方都意味着客户端和服务器分开。
    猜你喜欢
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 2014-01-28
    • 2020-09-08
    相关资源
    最近更新 更多