2

我正在尝试设置一个 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>

帐户控制器:

    [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);
    }
4

2 回答 2

7

在 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);
    }
于 2012-12-05T19:12:35.327 回答
0

我的登录工作正常。是我的注销不起作用。无论如何,我将 WebSecurity.Logout() 更改为 FormsAuthentication.SignOut() 并且有效。

于 2014-06-12T18:32:19.583 回答