在我的 MVC5 应用程序中,我曾经通过仅检查用户是否存在于Active Directory
. 现在,我想使用另一种类似的方法:我将username
and发送password
到active directory
,如果用户存在于其中,它应该返回一些active directory
信息,即用户的Name
, Surname
, Department
。那么,如何在Controller
和中定义这种身份验证web.config
?
网络配置:
<configuration>
<system.web>
<httpCookies httpOnlyCookies="true" />
<authentication mode="Forms">
<forms name=".ADAuthCookie" loginUrl="~/Account/Login" timeout="45" slidingExpiration="false" protection="All" />
</authentication>
<membership defaultProvider="ADMembershipProvider">
<providers>
<clear />
<add name="ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider" connectionStringName="ADConnectionString" attributeMapUsername="sAMAccountName" connectionUsername="myadmin@company" connectionPassword="MyPassword" />
</providers>
</membership>
</system.web>
<connectionStrings>
<!-- for LDAP -->
<add name="ADConnectionString" connectionString="LDAP://adadfaf.my.company:111/DC=my,DC=company" />
</connectionStrings>
</configuration>
控制器:
[AllowAnonymous]
[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult Login(User model, string returnUrl)
{
if (!this.ModelState.IsValid)
{
return this.View(model);
}
//At here I need to retrieve some user data from Active Directory instead of hust a boolean result
if (Membership.ValidateUser(model.UserName, model.Password))
{
//On the other hand I am not sure if this cookie lines are enough or not. Should I add some additional lines?
FormsAuthentication.SetAuthCookie(model.UserName, false);
if (this.Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return this.Redirect(returnUrl);
}
return this.RedirectToAction("Index", "Issue");
}
TempData["message"] = "The user name or password provided is incorrect.";
return this.View(model);
}