Login with Identity in MVC 5

2

I'm using MVC 5 and Identity for Login. I have a User class and I want to login with it, using the properties of this class for example the Property registry (User) instead of UserName (IdentityUser).

I have the User Class that inherits from IdentityUser and controller is the default Identity

User Class:

 public class Usuario:IdentityUser
{
    public int FuncionarioID { get; set; }

    public string Nome { get; set; }

    public string Registro { get; set; }

    public string Status { get; set; }
}

Contoller / AccountController.cs

public ActionResult Register()
    {
        return View();
    }

    //
    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser() { UserName = model.UserName };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInAsync(user, isPersistent: false);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                AddErrors(result);
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

I want to pass the User data to register here:

var user = new ApplicationUser() { UserName = model.UserName };
var result = await UserManager.CreateAsync(user, model.Password);

And to sign in too:

//
    // GET: /Account/Login
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }

    //
    // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindAsync(model.Registro, model.Password);
            if (user != null)
            {
                await SignInAsync(user, model.RememberMe);
                return RedirectToLocal(returnUrl);
            }
            else
            {
                ModelState.AddModelError("", "Registro ou Senha Invalidas!");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

Passing the User Class data here:

var user = await UserManager.FindAsync(model.Registro, model.Password);
    
asked by anonymous 18.09.2014 / 05:06

1 answer

3

Within your App_Start\IdentityConfig.cs file, modify:

public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    {
    }

    ...

To:

public class ApplicationUserManager : UserManager<Usuario>
{
    public ApplicationUserManager(IUserStore<Usuario> store)
        : base(store)
    {
    }

    ...

Just like your Usuario class, ApplicationUser is also derived from IdentityUser .

The login procedure is the same if you were using ApplicationUser :

var result = await SignInManager.PasswordSignInAsync(login, senha, lembrarme, shouldLockout: false);

result is a Microsoft.AspNet.Identity.Owin.SignInStatus (enum).

    
18.09.2014 / 18:49