【发布时间】: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 TransportCredentialOnly not sending username and password
Windows Authentication / Encryption in WCF With NetTcpBinding
【问题讨论】:
标签: c# .net wcf validation