asp.net-mvcsimplemembershipasp.net-mvc-5

SimpleMembership - UserManager.FindAsync does not work in MVC5


The following code does not work:

        if (ModelState.IsValid)
        {
            var user = await UserManager.FindAsync(model.UserName, model.Password);
            if (user != null)
            {
                await SignInAsync(user, model.RememberMe);
                return RedirectToLocal(returnUrl);
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password.");
            }
        }

user is null. model has correct values.

I change this code to:

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

and it works fine. Why and how to fix?


Solution

  • UserManager.FindAsync is a method call to a ASP.NET Identity method. WebSecurity.Login is a SimpleMembership method. ASP.NET Identity is not compatible with SimpleMembership. If you are using Asp.net identity with entity framework it will expect tables in your database that are different to the tables you were using with SimpleMembership.

    See http://www.asp.net/identity for more information on ASP.NET Identity.