Example #1
0
        public async Task <AOResult <AuthorizationTokenResponse> > RegisterAsync(RegistrationRequest request)
        {
            return(await BaseInvokeAsync(async() =>
            {
                if (_myHelperDbContext.AppUsers.Any(x => x.Email == request.Email || x.Username == request.UserName))
                {
                    return AOBuilder.SetError <AuthorizationTokenResponse>(Constants.Errors.UserAlreadyRegistered);
                }

                var appUser = new AppUser
                {
                    Username = request.UserName,
                    Email = request.Email,
                    Password = HashPasswordHelper.Hash(request.Password),
                    UserRole = EUserRole.User,
                    CreatedDate = DateTime.Now
                };

                await _myHelperDbContext.AddAsync(appUser);
                await _myHelperDbContext.SaveChangesAsync();

                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));
        }
Example #2
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));
        }
        public void GetAuthorizationTokenCanBeMocked()
        {
            var response = new AuthorizationTokenResponse();

            this.mockInStoreClient.Setup(mwsc =>
                                         mwsc.GetAuthorizationToken(It.IsAny <string>(), It.IsAny <string>(),
                                                                    It.IsAny <Dictionary <string, string> >())).Returns(response);

            var result = this.mockInStoreClient.Object.GetAuthorizationToken("123456789", "ASFW3OT8A35468");

            Assert.That(result, Is.EqualTo(response));
            this.mockInStoreClient.Verify(mwsc => mwsc.GetAuthorizationToken(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Dictionary <string, string> >()), Times.Once);
        }
Example #4
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));
        }