简体   繁体   English

使用存储库数据库的WCF自定义用户名和密码验证程序c#

[英]WCF Custom Username and Password Validator using a Repository Database c#

I have a WCF service that is configured programmatically - No XML config file. 我有一个以编程方式配置的WCF服务 - 没有XML配置文件。 I am trying to implement a Custom Username Password Validation using a repository database table to verify against. 我正在尝试使用存储库数据库表来实现自定义用户名密码验证以进行验证。 I do not wish to require Windows Usernames / passwords on client - server. 我不希望在客户端 - 服务器上要求Windows用户名/密码。 I am using NetTCPBinding. 我正在使用NetTCPBinding。

I know how to create a CustomValidator, and how to assign it to the ServiceBehavior. 我知道如何创建CustomValidator,以及如何将其分配给ServiceBehavior。 My issue is that I do not know how to set the RepositoryConnection string for the validator when the service instantiates it (I am not sure how that mechanism works anyway.) this property of my validator is what I need to set - programmatically - Please remember I am not using any XML CONFIG FILE . 我的问题是,当服务实例化时,我不知道如何为验证器设置RepositoryConnection字符串(我不知道该机制是如何工作的。)我的验证器的这个属性是我需要设置的 - 编程 - 请记住我没有使用任何XML CONFIG文件。 public string RepositoryConnection { get; public string RepositoryConnection {get; set;} 组;}

I know my NetTCPBinding will need to be changed and as this is my first WCF Security Attempt any comments are welcome and I will try to update question to provide any further info requested. 我知道我的NetTCPBinding需要更改,因为这是我的第一次WCF安全尝试,欢迎任何评论,我会尝试更新问题以提供任何进一步的信息。 I post relevant parts of code below. 我在下面发布了相关的代码部分。

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

Here is where I have looked for answers - aside from gargling it. 这是我寻找答案的地方 - 除了漱口。

WCF Authentication: Custom Username and Password Validator asp.net WCF身份验证:自定义用户名和密码验证程序asp.net

WCF custom username and password validation is not executed 不执行WCF自定义用户名和密码验证

WCF netTCPBinding WCF netTCPBinding

WCF TransportCredentialOnly not sending username and password WCF TransportCredentialOnly不发送用户名和密码

Windows Authentication / Encryption in WCF With NetTcpBinding 使用NetTcpBinding在WCF中进行Windows身份验证/加密

I have taken the 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);

So you may pass repository name/connstring as constructor parameter, or initialize validator prior to assign it to the host property. 因此,您可以将存储库名称/ connstring作为构造函数参数传递,或者在将其分配给主机属性之前初始化验证程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM