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