Login with Identity no MVC 5

Asked

Viewed 1,474 times

2

I’m using MVC 5 and Identity for Login. I have a User class and want to log in with it, using the properties of this class for example the Record(User) property instead of the Username(Identityuser).

I have the User Class that I inherit from Identityuser and controller is default from 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 log 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 User Class data here:

var user = await UserManager.FindAsync(model.Registro, model.Password);
  • You could put some code of what you have already implemented?

  • I entered the User Class. I want to pass Registration to login instead of Identity Username.

1 answer

3


Inside your file App_Start\IdentityConfig.cs, 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 class of Usuario, 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).

  • And as I would in the control for it to log in with User Class data?

  • I’ll update the answer.

  • Gypsy, I’m using Identity 1.0 that doesn’t have the Identityconfig.Cs class

  • @Janderson I need you to detail more your structure in the question so that my answer is more objective.

  • I edited the question!

  • From what I saw, 1.0 does not give the option to extend the user. It would not be better to update your ASP.NET Identity to version 2.0?

  • Could be, like it would be in 2.0?

  • Well, my answer takes a good look at the 2.0 scenario.

  • Making those changes I can already log in and log in?

  • @Janderson Can do it.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.