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?
– Leonel Sanches da Silva
I entered the User Class. I want to pass Registration to login instead of Identity Username.
– Janderson Thomaz