miyamotogL
miyamotogL

Reputation: 513

MVC4 Forms Authentication against AD

I am trying to setup an intranet MVC app that authenticates against our company's AD with forms authentication; we do want the user to have to login. I am getting the following exception when posting to the Login action: "To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider"." Anyone else had this problem?

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>

AccountController:

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

Upvotes: 2

Views: 6457

Answers (2)

kickinchicken
kickinchicken

Reputation: 1625

My login was working. It was my logout that wasn't working. In any case, I changed WebSecurity.Logout() to FormsAuthentication.SignOut() and that worked.

Upvotes: 0

miyamotogL
miyamotogL

Reputation: 513

Created an MVC3 site in VS2010 and got it working with the ActiveDirectoryMembershipProvider. Then changed the MVC4 AccountController to use the old System.Web.Security instead of WebMatrix.WebData.WebSecurity.

from:

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

to:

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

Upvotes: 7

Related Questions