Well, I have an application being developed in asp net mvc, in the same the user is logged in and after entering it is displayed the message "Hello, so please, I had to change some things in my AccountController, and then I found the problem, because I no longer allow after that change that I log in using email and password, it's only right if I put the username and password, plus the company's determination that the login be done only with email and password. I'll show below all the codes that involve this problem.
We start with _PartialView:
@Html.ActionLink("Olá " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
Next we have the AccountViewModels:
public class RegisterViewModel
{
[Required(ErrorMessage = "O nome completo deve ser informado.")]
[Display(Name = "Informe o seu nome completo")]
public string NomeCompleto { get; set; }
[Required(ErrorMessage = "O nome da empresa deve ser informado.")]
[Display(Name = "Informe o nome da sua empresa")]
public string EmpresaNome { get; set; }
[Required(ErrorMessage = "O número do telefone deve ser informado.")]
[Display(Name = "Informe o número do telefone")]
public string Telefone { get; set; }
[Required(ErrorMessage = "O email deve ser informado.")]
[EmailAddress(ErrorMessage = "Informe um email válido.")]
[Display(Name = "Email")]
public string Email { get; set; }
[Required(ErrorMessage = "A senha dessa ser informada")]
[StringLength(100, ErrorMessage = "A {0} deve ter pelo menos {2} caracteres.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "A senha e a senha de confirmação não correspondem.")]
public string ConfirmPassword { get; set; }
}
And in AccountController we have the login and registration classes:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser {
NomeCompleto = model.NomeCompleto,
UserName = model.NomeCompleto,
Email = model.Email,
EmpresaNome = model.EmpresaNome,
Telefone = model.Telefone
};
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
Here is the login class:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
TempData["mensagemErro"] = "Login inválido! Por favor verifique se digitou sua senha ou email corretos!";
return RedirectToAction("Login", "Account");
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToAction("Index", "Home");
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
TempData["mensagemErro"] = "Login inválido! Por favor verifique se digitou sua senha ou email corretos!";
return RedirectToAction("Login");
}
}
If someone can help me with this I will be extremely grateful, I am in the dilemma, if I display the user name instead of the email on the welcome screen I can not log in using the email and vice versa !!!!