Exemplo n.º 1
0
        public async Task <IActionResult> Login([FromBody] LogInUserDTO body)
        {
            User user = await manager.FindByNameAsync(body.Username);

            if (user != null && await manager.CheckPasswordAsync(user, body.Password))
            {
                var tokenDescriptor = new SecurityTokenDescriptor
                {
                    Subject = new ClaimsIdentity(new Claim[]
                    {
                        new Claim("UserID", user.Id.ToString()),
                        new Claim("Role", user.Role),
                    }),
                    Expires = DateTime.UtcNow.AddDays(1)
                };
                var tokenHandler  = new JwtSecurityTokenHandler();
                var securityToken = tokenHandler.CreateToken(tokenDescriptor);
                var token         = tokenHandler.WriteToken(securityToken);
                return(Ok(new { token, role = user.Role, name = user.FullName, userId = user.Id.ToString() }));
            }
            else
            {
                return(BadRequest(new { message = "Username or password is incorrect." }));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> LogIn(LogInUserDTO user)
        {
            var result = await _userService.AuthenticateAsync(user);

            if (!result.IsSuccess)
            {
                return(BadRequest(result.Descriptions));
            }
            var encodedJwt = GetToken(user.Login);
            var response   = new
            {
                access_token = encodedJwt,
                username     = ((string[])result.Descriptions)[0]
            };

            return(Ok(response));
        }
        public async Task <OperationDetails> AuthenticateAsync(LogInUserDTO userDTO)
        {
            if (!userDTO.IsValid())
            {
                return(userDTO.GetValidateError());
            }
            string username        = null;
            var    applicationUser = await userManager.GetUserByEmailAsync(userDTO.Login);

            if (applicationUser == null)
            {
                var userProfile = await dataStore.UserProfiles.GetByNameAsync(userDTO.Login);

                if (userProfile == null)
                {
                    return(new OperationDetails(false, new string[] { "Username not found." }));
                }
                username        = userProfile.Username;
                applicationUser = await userManager.GetUserByIdAsync(userProfile.IdentityUserId);

                if (applicationUser == null)
                {
                    return(new OperationDetails(false, new string[] { "Error in system logic." }));
                }
            }
            var result = await userManager.CheckPassword(applicationUser, userDTO.Password);

            if (!result.IsSuccess)
            {
                return(new OperationDetails(false, result.ErrorList.Select(x => x.Description)));
            }
            else
            {
                if (username == null)
                {
                    username = dataStore.UserProfiles.GetAll()
                               .FirstOrDefault(x => x.IdentityUserId == applicationUser.Id)
                               .Username;
                }
                ///включение подтверждения пароля
                //if(applicationUser.EmailConfirmed)
                return(new OperationDetails(true, new string[] { username }));
                //else
                //  return new OperationDetails(false, new string[] { "Email not confirmed." });
            }
        }