【问题标题】:Alternate ways WCF Authentication with netTcpBinding binding使用 netTcpBinding 绑定的替代方法 WCF 身份验证
【发布时间】:2015-09-03 11:23:30
【问题描述】:

我正在为 Intranet 应用程序编写 WCF 服务器。我是 WCF 的新手,对身份验证有一些疑问。对于此服务器,绑定将为 netTcp,建议使用带有 Windows 身份验证的 netTcpBinding。但是对于我的要求,我需要一个自定义登录,用户将使用他自己的凭据而不是 Windows 凭据进行验证。
我计划以这种方式实施服务合同并验证登录凭据

*[ServiceContract(SessionMode = SessionMode.Required)]
public interface Iservice
{
    [OperationContract]
    String Login(String username, String password);
}   
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession) ]
public class serviceclass : Iservice
{
    String Login(String username, String password);
    {
        //Validate uname and password with DB.
        if (validate)
            return OperationContext.Current.SessionId;
        else return String.Empty;   
    }
}*
Is this a good approach or is there a better approach to acheive this.

请指导。

【问题讨论】:

    标签: wcf authentication nettcpbinding


    【解决方案1】:

    我建议您使用UserNamePasswordValidator 进行自定义验证,它符合您的要求。

    您只需要像这样从 UserNamePasswordValidator 类继承和实现验证:

    public class CustomUserNameValidator : UserNamePasswordValidator
    {
      public override void Validate(string userName, string password)
      {
        if (null == userName || null == password)
        {
            throw new ArgumentNullException();
        }
    
        if (!(userName == "test1" && password == "1tset") && !(userName == "test2" && password == "2tset"))
        {
            // This throws an informative fault to the client.
            throw new FaultException("Unknown Username or Incorrect Password");
            // When you do not want to throw an infomative fault to the client,
            // throw the following exception.
            // throw new SecurityTokenException("Unknown Username or Incorrect Password");
        }
       }
    }
    

    您可以替换静态用户名和密码并在您的数据存储中进行验证。

    服务配置:

    <serviceBehaviors>
      <behavior name="CustomValidator">
        <serviceCredentials>
          <userNameAuthentication
            userNamePasswordValidationMode="Custom"
            customUserNamePasswordValidatorType=
               "MyAssembly.CustomUserNameValidator, MyAssembly"/>      
        </serviceCredentials>
      </behavior>
    <serviceBehaviors>
    
    <netTcpBinding>
      <binding name="tcpWithMessageSecurity">
        <security mode="Message" >
          <message clientCredentialType="UserName"/>
        </security>
      </binding>
    </netTcpBinding>
    

    对于客户端,您可以使用凭据来填充用户名和密码。

    proxy.ClientCredentials.UserName.UserName = "user";
    proxy.ClientCredentials.UserName.Passsord = "password";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-30
      • 2013-07-10
      • 2011-01-13
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多