3
我正在使用标准的简单成员资格模型登录通过我的应用程序中的窗体。我想提供通过AD登录作为替代的可能性。使用Active Directory登录使用本地用户
当经由AD登录,这个过程应该是如下:
- 检查AD对用户进行认证,但不使用用于主要的信息。
- 检查是否有任何本地用户存在提供的Active Directory用户名(我在我的UserProfile模型上有一个名为ActiveDirectoryID的属性)。
- 如果存在,请使用本UserProfile的本地用户名执行本地登录。
问题:我无法检索本地密码,所以为了在AD身份验证后本地登录,我需要能够强制登录而不使用密码。
我已经考虑以下策略:
- 创建一个扩展方法Websecurity允许Websecurity.Login(用户名字符串)
- 莫名其妙定置登录的用户手动,没有牵连Websecurity。
这是可行的吗?框架是否有可能在没有明文密码的情况下创建必要的身份验证cookie?我该怎么做?
SOLUTION:
就这样结束是正确的解决方案:
public ActionResult ActiveDirectoryLogin(LoginModel model, string returnUrl)
{
if (ModelState.IsValid)
{
try
{
DirectoryEntry entry = new DirectoryEntry("LDAP://DC=MyIntranet,DC=MyCompany,DC=com", model.UserName, model.Password);
object NativeObject = entry.NativeObject;
var internalUser = db.UserProfiles.Where(x => x.ActiveDirectoryID == model.UserName).SingleOrDefault();
if (internalUser != null)
{
FormsAuthentication.SetAuthCookie(internalUser.UserName, model.RememberMe);
return RedirectToLocal(returnUrl);
}
}
catch (DirectoryServicesCOMException)
{
// No user existed with the given credentials
}
catch (InvalidOperationException)
{
// Multiple users existed with the same ActiveDirectoryID in the database. This should never happen!
}
}
return RedirectToAction("Login");
}
辉煌。你肯定会把我放在正确的轨道上,尽管WebSecurity由于是静态的而变成不可扩展的。我在上面的答案中提供了最终解决方案。 –