在 WCF 中进行自定义用户名密码身份验证的最佳方法是什么?
我有以下代码(基于网络上的大量示例)
public class UserNameValidator : UserNamePasswordValidator
{
/// <summary>
/// Validates the user name and password combination.
/// </summary>
/// <param name="userName">The user name.</param>
/// <param name="password">The password.</param>
public override void Validate(string userName, string password)
{
// validate arguments
if (string.IsNullOrEmpty(userName))
throw new ArgumentNullException("userName");
if (string.IsNullOrEmpty(password))
throw new ArgumentNullException("password");
UserCredential user = InMemoryUserStore.Get(userName);
if (user == null)
{
using (DataAccessAdapter da = new DataAccessAdapter())
{
LinqMetaData db = new LinqMetaData(da);
var newUserCredential = (from u in db.User
where u.Username == userName
select new UserCredential
{
UserName = u.Username,
PasswordHash = u.PasswordHash,
PasswordSalt = u.PasswordSalt
}).FirstOrDefault();
if (newUserCredential == null)
{
throw new SecurityTokenException("Unknown username or password");
}
else
{
InMemoryUserStore.Add(newUserCredential);
user = newUserCredential;
}
}
}
//Validate Password
PasswordHash p = new PasswordHash(user.PasswordSalt, user.PasswordHash);
if (!p.Verify(password))
{
throw new SecurityTokenException("Unknown username or password");
}
}
}
这是最好的方法吗?
I have the following code (based on numerous examples on the web)
public class UserNameValidator : UserNamePasswordValidator
{
/// <summary>
/// Validates the user name and password combination.
/// </summary>
/// <param name="userName">The user name.</param>
/// <param name="password">The password.</param>
public override void Validate(string userName, string password)
{
// validate arguments
if (string.IsNullOrEmpty(userName))
throw new ArgumentNullException("userName");
if (string.IsNullOrEmpty(password))
throw new ArgumentNullException("password");
UserCredential user = InMemoryUserStore.Get(userName);
if (user == null)
{
using (DataAccessAdapter da = new DataAccessAdapter())
{
LinqMetaData db = new LinqMetaData(da);
var newUserCredential = (from u in db.User
where u.Username == userName
select new UserCredential
{
UserName = u.Username,
PasswordHash = u.PasswordHash,
PasswordSalt = u.PasswordSalt
}).FirstOrDefault();
if (newUserCredential == null)
{
throw new SecurityTokenException("Unknown username or password");
}
else
{
InMemoryUserStore.Add(newUserCredential);
user = newUserCredential;
}
}
}
//Validate Password
PasswordHash p = new PasswordHash(user.PasswordSalt, user.PasswordHash);
if (!p.Verify(password))
{
throw new SecurityTokenException("Unknown username or password");
}
}
}
Is this the best way of doing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于自定义验证器仅被调用一次,因此不需要 InMemoryStore。下面的代码是我们正在使用的代码,它在生产中运行得很好。
调用经过身份验证后,您可以使用以下命令创建自定义主体:
Seeing as the Custom Validator is only called once, the InMemoryStore is not needed. The code below is what we are using, and it is working great in production.
Once the call is authenticated, you can create a Custom Principal using the following: