【问题标题】:MVC4 Forms Authentication against AD针对 AD 的 MVC4 表单身份验证
【发布时间】:2012-12-04 20:01:25
【问题描述】:

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

Web.config:

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

【问题讨论】:

  • 您在使用 WebMatrix 吗?我自己没用过,难道你不能将WebMatrix与ActiveDirectory一起使用吗?如果您不添加 enableSimpleMembership 密钥,您还会遇到问题吗?查看使用 DotPeek 的 WebSecurity 类的代码,我可以看到这条评论,它与您的应用设置不太匹配:// Allow use of &lt;add key="EnableSimpleMembershipKey" value="false" /&gt; to disable registration of membership/role providers as default.
  • 这是一个全新的项目,使用 VS2012 中的 MVC4 w/Forms Auth 模板。 "enableSimpleMembership" = true 时,仍然会抛出异常。
  • 将“enableSimpleMembership”更改为“EnableSimpleMembershipKey”也没有效果。

标签: asp.net-mvc-4 forms-authentication


【解决方案1】:

在 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);
    }

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-09-25
      • 1970-01-01
      • 2015-05-13
      • 2020-10-14
      • 1970-01-01
      • 2012-03-06
      • 2020-01-14
      • 1970-01-01
      • 2012-05-21
      相关资源
      最近更新 更多