使用存储库数据库 c# 的 WCF 自定义用户名和密码验证器 [英] WCF Custom Username and Password Validator using a Repository Database c#

查看:20
本文介绍了使用存储库数据库 c# 的 WCF 自定义用户名和密码验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a WCF service that is configured programmatically - No XML config file. 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. I am using NetTCPBinding.

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

I know how to create a CustomValidator, and how to assign it to the 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 . public string RepositoryConnection { get; set;}

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

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. 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 身份验证:自定义用户名和密码验证器 asp.net

WCF 自定义用户名和密码验证未执行

WCF netTCPBinding

WCF TransportCredentialOnly 不发送用户名和密码

WCF 中使用 NetTcpBinding 的 Windows 身份验证/加密

推荐答案

我已经采取了 此处示例

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.

这篇关于使用存储库数据库 c# 的 WCF 自定义用户名和密码验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆