Ejemplo n.º 1
0
        public async Task <ServerResponse <AuthorizationTokenResponse> > LoginAsync(LoginRequest request)
        {
            return(await BaseInvokeAsync(async() =>
            {
                var appUser = await DbContext.AppUsers
                              .AsQueryable()
                              .FirstOrDefaultAsync(x => x.Username == request.UserName);

                if (appUser == null)
                {
                    throw new UnauthorizedException(Constants.Errors.UsernameIsIncorrect);
                }
                if (!HashPasswordHelper.Verify(appUser.Password, request.Password))
                {
                    throw new UnauthorizedException(Constants.Errors.PasswordIsIncorrect);
                }

                var tokenInfo = _tokenService.CreateToken(GetClaimsFromAppUser(appUser));

                var authorizationTokenResponse = new AuthorizationTokenResponse()
                {
                    Token = tokenInfo.Token,
                    ExpirationDate = tokenInfo.ExpiredDate,
                    AppUserViewModel = Mapper.Map <AppUser, AppUserViewModel>(appUser)
                };

                return ServerResponseBuilder.Build(authorizationTokenResponse);
            }, request));
        }
Ejemplo n.º 2
0
        public async Task <AOResult <AuthorizationTokenResponse> > LoginAsync(LoginRequest request)
        {
            return(await BaseInvokeAsync(async() =>
            {
                var appUser = await _myHelperDbContext.AppUsers
                              .FirstOrDefaultAsync(x => x.Username == request.UserName);

                if (appUser == null || !HashPasswordHelper.Verify(appUser.Password, request.Password))
                {
                    return AOBuilder.SetError <AuthorizationTokenResponse>("Username or password is incorrect");
                }

                var tokenInfo = _tokenService.CreateToken(GetClaimsFromAppUser(appUser));

                var authorizationTokenResponse = new AuthorizationTokenResponse()
                {
                    Token = tokenInfo.Token,
                    ExpirationDate = tokenInfo.ExpiredDate,
                    AppUserViewModel = _mapper.Map <AppUser, AppUserViewModel>(appUser)
                };

                return AOBuilder.SetSuccess(authorizationTokenResponse);
            }, request));
        }