gpt4 book ai didi

asp.net-mvc-4 - 针对 AD 的 MVC4 表单例份验证

转载 作者:行者123 更新时间:2023-12-04 06:06:09 25 4
gpt4 key购买 nike

我正在尝试设置一个 Intranet MVC 应用程序,该应用程序使用表单例份验证对我们公司的 AD 进行身份验证;我们确实希望用户必须登录。发布到登录操作时出现以下异常:“要调用此方法,“Membership.Provider”属性必须是“ExtendedMembershipProvider”的实例。”还有其他人有这个问题吗?

网络配置:

<connectionStrings>
<add name="ADConnectionString" connectionString="LDAP://example.domain.com/DC=example,DC=domain,DC=com"/>
</connectionStrings>
<appSettings>
<add key="enableSimpleMembership" value="false" />
</appSettings>
<system.web>
<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"/>
</providers>
</membership>

账户 Controller :

    [HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
//The call to WebSecurity.Login is what throws
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
return RedirectToLocal(returnUrl);
}

// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}

最佳答案

在 VS2010 中创建了一个 MVC3 站点,并让它与 ActiveDirectoryMembershipProvider 一起工作。然后将 MVC4 AccountController 更改为使用旧的 System.Web.Security 而不是 WebMatrix.WebData.WebSecurity。

来自:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
return RedirectToLocal(returnUrl);
}

// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}

到:

    [HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{

if (ModelState.IsValid && Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}

关于asp.net-mvc-4 - 针对 AD 的 MVC4 表单例份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13710938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com