【问题标题】:WCF Custom Username and Password Validator using a Repository Database c#使用存储库数据库 c# 的 WCF 自定义用户名和密码验证器
【发布时间】:2016-02-11 16:46:36
【问题描述】:

我有一个以编程方式配置的 WCF 服务 - 没有 XML 配置文件。 我正在尝试使用存储库数据库表来实现自定义用户名密码验证以进行验证。我不希望在客户端 - 服务器上要求 Windows 用户名/密码。我正在使用 NetTCPBinding。

我知道如何创建一个 CustomValidator,以及如何将它分配给 ServiceBehavior。 我的问题是,当服务实例化它时,我不知道如何为验证器设置 RepositoryConnection 字符串(我不确定该机制是如何工作的。) 我的验证器的这个属性是我需要设置的 - 以编程方式 - 请记住我没有使用任何 XML CONFIG FILE。 公共字符串 RepositoryConnection { get;设置;}

我知道我的 NetTCPBinding 需要更改,因为这是我的第一次 WCF 安全尝试,欢迎任何 cmets,我将尝试更新问题以提供所需的任何进一步信息。我在下面发布了代码的相关部分。

public static NetTcpBinding SetNetTCPBinding(string name, string ns)
{
    NetTcpBinding binding = new NetTcpBinding(); //SecurityMode.None);
    binding.Security.Mode = SecurityMode.Transport;
    binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
    binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
    binding.Security.Transport.ProtectionLevel = ProtectionLevel.None;
}


public class ServiceUserNamePasswordValidator : UserNamePasswordValidator
{
    **public string RepositoryConnection { get; set;}**

    public override void Validate(string userName, string password)
    {
            try
            {
                if (string.IsNullOrEmpty(userName) || password == null)
                {
                    //throw new ArgumentNullException();
                    throw new FaultException("Username cannot be null or empty; Password cannot be null and should not be empty");
                }
                IGenericDataRepository<MyAppDBData.Users> repositorySingle = new GenericDataRepository<MyAppDBData.Users>(RepositoryConnection);
                MyAppDBData.Users USER = new MyAppDBData.Users();

                USER = repositorySingle.GetSingle(d => d.Name.ToLower() == userName.ToLower() && d.Password == password);

                if (USER == null)
                {
                    // This throws an informative fault to the client.
                    throw new FaultException("Unknown Username or Incorrect Password");
                }
            }
            finally{
            }
   }
}

这是我寻找答案的地方——除了漱口。

WCF Authentication: Custom Username and Password Validator asp.net

WCF custom username and password validation is not executed

WCF netTCPBinding

WCF TransportCredentialOnly not sending username and password

Windows Authentication / Encryption in WCF With NetTcpBinding

【问题讨论】:

    标签: c# .net wcf validation


    【解决方案1】:

    我已经拿了sample here

    WebServiceHost host = new ServiceHost(typeof(CloudService), httpUri);
    host.Description.Behaviors.Find<ServiceCredentials>().UserNameAuthentication
        .UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
    host.Description.Behaviors.Find<ServiceCredentials>().UserNameAuthentication
        .CustomUserNamePasswordValidator = new ServiceUserNamePasswordValidator(repositoryConnectionString);
    

    因此您可以将存储库名称/connstring 作为构造函数参数传递,或者在将验证器分配给主机属性之前对其进行初始化。

    【讨论】:

    • 我在那个页面上看到了 x509 的东西,这让我很震惊。我有这个 - 我应该改变任何东西 binding.Security.Mode = SecurityMode.Transport; // 我应该改成(With Message Credential)?; binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows; binding.Security.Transport.ProtectionLevel = ProtectionLevel.None;
    • 我认为这里msdn.microsoft.com/en-us/library/aa702565.aspx 的所有有效模式/ClientCredentialType 组合都很好地描述了。简而言之“消息安全 -> 用户名”,“传输安全 -> 基本”
    猜你喜欢
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    相关资源
    最近更新 更多