0
在我的MVC5应用程序中,我通过检查用户是否存在或不在Active Directory
中来应用以下方法。现在,我想用另一种方法这样的:我送username
和password
到active directory
如果用户在它的存在,它应该返回一些active directory
信息,即用户的Name
,Surname
,Department
。那么,如何在Controller
和web.config
中定义这种认证?在MVC中检索用户数据的Active Directory身份验证
的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="[email protected]" 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);
}
非常感谢您的回复。我尝试应用此方法,但遇到了一些问题,即SesssionUser类中的“名称'SearchPattern'在当前上下文中不存在”。另一方面,CurrUser = false;行不应该工作。我们应该分配空而不是假吗?因为用户不是布尔类型,并且它是一个域类。 –
这是我为我的公司创建的一个类也是User类。 Iam不允许将它写入堆栈。在'ValidateUser()'方法中,您必须为用户提供针对AD的自己的验证。你可以看看[codeproject](http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C) – C0d1ngJammer
我改变了代码。正如我所说,你必须创建自己的AD-Class才能获得公司,姓...... – C0d1ngJammer