0

我必须实现这样的接口:

interface IMembershipWrapper
{
  Guid GetUserId();
  Guid GetUserId(string username, bool userIsOnline);
  bool ValidateUser(string userName, string password);
    …
}

针对活动目录并使用 Unity 注入它。

我可能会为某些方法抛出 NotImplementedException 异常,但您认为这通常是可能的吗?你推荐什么策略?

我知道我可以通过 web.config 配置“活动目录 asp.net 表单身份验证”,如此处所述。不幸的是,这不是一个选择。

4

1 回答 1

3

这应该是完全可能的,无需更改 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)使用ValidateCredentials()_PrinicalContext

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。

于 2013-03-22T16:14:21.253 回答