Exemplo n.º 1
0
        public async Task <ActionResult> Login([FromBody] LoginViewModel model, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                return(Json(model));
            }

            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                return(Ok(new
                {
                    authenticated = false,
                    message = "You are unautorized to access this site."
                }));
            }
            if (!user.EmailConfirmed)
            {
                return(Ok(new
                {
                    authenticated = false,
                    message = "User is registered and can login after activation."
                }));
            }

            var result = await _signInManager.PasswordSignInAsync(user, model.Password, model.RememberMe, false);

            var roles = await _userManager.GetRolesAsync(user);

            var token = TokenAuthOptions.GenerateToken(user.UserName);

            if (result.Succeeded)
            {
                return(Ok(new
                {
                    authenticated = true,
                    token = token,
                    user = user,
                    message = "Welcome to the Identity"
                }));
            }
            if (result.IsLockedOut)
            {
                return(BadRequest("User account locked out."));
            }
            return(BadRequest("Something wen't wrong."));
        }