我正在開發一個MVC3應用程序,該應用程序將要求用戶針對AD進行身份驗證。我知道MVC3中有一個選項可以創建一個Intranet應用程序,該應用程序可以根據AD自動驗證用戶身份,但它使用Windows身份驗證並自動將其登錄。這個應用程序可以在'Open'工作站上訪問,用戶需要輸入他們的用戶名和密碼。任何示例或在線教程都會很棒。一個示例項目將是例外。ASP.NET MVC - 根據Active Directory對用戶進行身份驗證,但要求輸入用戶名和密碼
回答
您可以使用窗體身份驗證標準的Internet應用程序模板,並插入一個ActiveDirectoryMembershipProvider
到web.config
:
<connectionStrings>
<add name="ADConnectionString" connectionString="LDAP://YOUR_AD_CONN_STRING" />
</connectionStrings>
<system.web>
<authentication mode="Forms">
<forms name=".ADAuthCookie" loginUrl="~/Account/LogOn"
timeout="15" slidingExpiration="false" protection="All" />
</authentication>
<membership defaultProvider="MY_ADMembershipProvider">
<providers>
<clear />
<add name="MY_ADMembershipProvider"
type="System.Web.Security.ActiveDirectoryMembershipProvider"
connectionStringName="ADConnectionString"
attributeMapUsername="sAMAccountName" />
</providers>
</membership>
</system.web>
這樣你得到的互聯網應用程序模板登錄表單,並驗證對AD爲您服務。
然後這只是一些AccountController
清理的問題,以刪除重置密碼/更改密碼/註冊功能,只留下登錄。
如上所述,您可以使用web.config文件中定義的成員資格提供程序。
下面的代碼是從MVC 3模板代碼「的AccountController」的實施中,並已略作修改與ActiveDirectory的工作:
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if(ModelState.IsValid)
{
// Note: ValidateUser() performs the auth check against ActiveDirectory
// but make sure to not include the Domain Name in the User Name
// and make sure you don't have the option set to use Email Usernames.
if(MembershipService.ValidateUser(model.UserName, model.Password))
{
// Replace next line with logic to create FormsAuthenticationTicket
// to encrypt and return in an Http Auth Cookie or Session Cookie
// depending on the 'Remember Me' option.
//FormsService.SignIn(model.UserName, model.RememberMe);
// Fix this to also check for other combinations/possibilities
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
如果使用.NET 3.5 - 然後閱讀this article爲替代方案:
我認爲它應該是Membership.ValidateUser而不是MembershipService.ValidateUser – r03 2012-11-20 10:39:50
cpoDesign可能使用他自己的服務來驗證用戶,即'MembershipService'。在它內部,他可能使用'MembershipService.ValidateUser'。 – 2013-03-25 12:48:19
- 1. 通過Active Directory對用戶進行身份驗證,但要求輸入用戶名和密碼
- 2. 身份驗證的Active Directory用戶沒有要求的用戶名和密碼
- 3. 如何根據Azure Active Directory對用戶進行身份驗證
- 4. 身份驗證要求輸入用戶名和密碼http:// www
- 5. 無需輸入用戶名和密碼進行身份驗證
- 6. Windows mobile C#根據Active Directory驗證用戶名和密碼?
- 7. 試圖根據AD對用戶進行身份驗證 - ASP.NET MVC
- 8. Asp.net MVC Windows身份驗證將要求用戶在使用Firefox時輸入他們的用戶名和密碼
- 9. 通過ADFS(Active Directory Federation Service)對用戶進行身份驗證
- 10. 使用Active Directory進行身份驗證
- 11. 如何根據Active Directory聯合身份驗證服務(ADFS)驗證用戶名和密碼?
- 12. PHP和Active Directory身份驗證允許輸入錯誤密碼
- 13. 用戶使用Azure Active Directory進行身份驗證後執行代碼
- 14. 使用Active Directory進行身份驗證和ASP.NET身份驗證角色
- 15. 用戶在CMS中使用Active Directory進行身份驗證
- 16. 在MVC中檢索用戶數據的Active Directory身份驗證
- 17. 如何以用戶身份對Azure Active Directory圖形API進行身份驗證?
- 18. 使用Xamarin.Auth進行OAuth2身份驗證 - 用戶名和密碼?
- 19. Windows身份驗證提示輸入用戶名和密碼
- 20. ASP.NET MVC - 根據Active Directory自動驗證用戶?
- 21. Windows身份驗證提示輸入用戶名/密碼
- 22. ASP.NET MVC中WIF(Windows身份基礎)的用戶名和密碼身份驗證
- 23. 要求經過身份驗證的用戶更改密碼
- 24. IBM針對Web用戶的Active Directory的Domino和身份驗證
- 25. mvc 5僅限受限用戶使用登錄進行Active Directory身份驗證
- 26. ASP.NET - 檢測用戶是否使用Active Directory進行身份驗證?
- 27. 如何使用php和CodeIgniter對Active Directory進行身份驗證?
- 28. iis身份驗證用戶名密碼
- 29. Tweepy使用用戶名/密碼進行身份驗證
- 30. 如何使用ASP.NET網頁和Razor對Active Directory進行身份驗證?
我們如何爲MVC4做到這一點? – 2013-09-24 13:05:53
@JamesHarpe它是一樣的... – Rovdjuret 2014-02-25 13:46:14
它如何與ASP.NET Core(MVC 6)一起使用? – 2016-03-28 13:03:08