Login with email and not with username in Asp net mvc, how to resolve?

Asked

Viewed 71 times

0

Well, I have an application being developed in Asp net mvc, in the same the user logs in and after logging in is displayed the message " Hello so so so so so welcome", for that I had to change some things in my Accountcontroller, and then I found the problem, because I did not allow more after this change that I login using email and password, only sure if I put the username and password, plus the determination of the company that the login is done only with email and password. I will show below all the codes that involves 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 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 anyone can help me with this I will be extremely grateful, I am in the dilemma, if I display the name of the user instead of the email on the welcome screen I can not login using email and vice versa!!!!

  • 1

    Take a look at this answer https://answall.com/questions/271360/displayingmessaging goods-vindas-na-navbar/271433#271433

  • This question of mine here the context is different face.... la I could login using the username, in this case here now I am obliged to login with the email, is this the problem, understood???

No answers

Browser other questions tagged

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