Пример #1
0
        public UserAuthenticationResponse Execute(UserAuthenticationRequest request)
        {
            var user = new UserSearchHandler(uContext).Execute(new UserSearchRequest
            {
                Password = request.Password,
                UserName = request.UserName
            });

            if (user == null || !user.Any())
            {
                throw new AuthenticationFailedException("Authentication Failed", "User doesn't exists in system, Authentication failed");
            }

            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(ContractRegistration.JwtTokenKey);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[] {
                    new Claim(ClaimTypes.NameIdentifier, user.First().UserId.ToString()),
                    new Claim(ClaimTypes.Name, user.First().UserName)
                }),
                Expires            = DateTime.Now.AddDays(1),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature)
            };

            var token       = tokenHandler.CreateToken(tokenDescriptor);
            var tokenString = tokenHandler.WriteToken(token);

            return(new UserAuthenticationResponse
            {
                UserName = user.First().UserName,
                Token = tokenString
            });
        }
        public int Create(UserDto actionDto)
        {
            var user = new UserSearchHandler(uContext).Execute(new UserSearchRequest
            {
                Password = "",
                UserName = actionDto.UserName
            });

            if (user != null && user.Any())
            {
                throw new DuplicateException("Duplicate user", "Already there is user exsisting for this credentials.");
            }

            return(uContext.Save(Mapper.Map(actionDto)));
        }