示例#1
0
        /// <summary>
        /// Method used for user authorization. UserManager checks
        /// if user with the username exists and checks password.
        /// If authorization is successful, creates a token and returns
        /// it.
        /// </summary>
        /// <param name="model"></param>
        /// <returns>Authorization response DTO</returns>
        public async Task <AuthorizeResponseDto> Authorize(AuthorizeBm model)
        {
            var user = await userManager.FindByNameAsync(model.UserName);

            if (user != null && await userManager.CheckPasswordAsync(user, model.Password))
            {
                var token = await CreateTokenAsync(user);

                var response = mapper.Map <AuthorizeResponseDto>(token);
                response.UserName = user.UserName;

                if (response != null)
                {
                    logger.LogInfo($"AUTHORIZATION SUCCESSFUL : User {model.UserName} successfully authorized.");
                    return(response);
                }
            }

            throw new BadRequestException("Invalid username or password.");
        }
示例#2
0
        public async Task <IActionResult> Authorize(AuthorizeBm model)
        {
            var result = await AccountService.Authorize(model);

            return(Ok(result));
        }