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