コード例 #1
0
        public async Task <IActionResult> LoginAsync(LoginViewModel model)
        {
            var newModel = new LoginViewModel()
            {
                Mail = model.Mail
            };

            if (!api.Login(model.Password, model.Mail))
            {
                ModelState.AddModelError("mail", "El usuario o la contraseña son incorrectos");
                return(View("Login", newModel));
            }
            if (!api.IsUserVerified(model.Mail, model.Password))
            {
                ModelState.AddModelError("mail", "La cuenta no ha sido verificada");
                return(View("Login", newModel));
            }
            var user       = api.GetUserByEmail(model.Mail);
            var emailClaim = new Claim(ClaimTypes.Email, model.Mail);
            var roleClaim  = new Claim(ClaimTypes.Role, user.Role);
            var idClaim    = new Claim(ClaimTypes.Sid, user.Id.ToString());
            var nameClaim  = new Claim(ClaimTypes.Name, user.Name);
            var identity   = new ClaimsIdentity(new[] { emailClaim, roleClaim, nameClaim, idClaim }, "cookie");
            var principal  = new ClaimsPrincipal(identity);

            await this.HttpContext.SignInAsync(principal);

            User.FindFirstValue(ClaimTypes.Email);
            return(RedirectToAction("Index", ""));
        }
コード例 #2
0
        public async Task <IActionResult> GetToken([FromBody] UserLogin model)
        {
            if (!api.Login(model.Password, model.Mail))
            {
                return(StatusCode(400, "Los datos no corresponden a ningun usuario"));
            }
            if (!api.IsUserVerified(model.Mail, model.Password))
            {
                return(StatusCode(400, "La cuenta no ha sido verificada"));
            }
            var token = api.GetUserToken(model.Mail);

            await new EmailSender(emailOptions).SendEmailAsync(model.Mail, "Token MejorPrecio3", $"Su token de usuario para MejorPrecio3.com es: " + token);
            return(StatusCode(200, "Le enviamos su token de usuario por correo electronico."));
        }