我必须像这样实现一个接口:
interface IMembershipWrapper
{
Guid GetUserId();
Guid GetUserId(string username, bool userIsOnline);
bool ValidateUser(string userName, string password);
…
}
并使用Unity注入它。
对于某些方法,我可能会抛出一个NotImplementedException异常,但您认为这通常是可能的吗?你有什么推荐的策略?
我知道我可以通过web.config配置“active directory asp.net窗体身份验证”,如here所述。不幸的是,这不是一个选择。
发布于 2013-03-22 16:14:21
在不更改web.config中的身份验证系统的情况下,这应该是完全可能的。尤其是如果您使用的是.NET 3.5+。看看System.DirectoryServices.AccountManagement吧。
要实现GetUserId(string username, bool userIsOnline)
,您可能需要尝试执行以下操作:
public Guid GetUserId(string username, bool userIsOnline) {
using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "[active directory domain here]")) {
var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, username);
if(user != null)
return user.Guid.Value;
else
return null;
}
}
要实现ValidateUser(string userName, string password)
,请在PrinicalContext
上使用ValidateCredentials()
public bool ValidateUser(string userName, string password) {
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "[active directory domain here]"))
{
return pc.ValidateCredentials(userName, password);
}
}
如果没有更多关于您的实现的信息,我不确定如何实现GetUserId()
,因为您似乎没有足够的信息来访问Active Directory。
https://stackoverflow.com/questions/15574550
复制相似问题