Esempio n. 1
0
 private static ClaimsIdentity GetClaimsIdentity(JwtTokenContext jwtTokenContext)
 {
     return(new ClaimsIdentity(new[]
     {
         new Claim(ClaimTypes.NameIdentifier, jwtTokenContext.UserId.ToString()),
         new Claim(ClaimTypes.Name, jwtTokenContext.Login),
         new Claim(ClaimTypes.Role, jwtTokenContext.Role.GetDescription())
     }));
 }
Esempio n. 2
0
        public async Task <TokenModel> Login(LoginModel loginModel)
        {
            var user = await _userService.FirstOrDefaultAsync(a => a.Login == loginModel.Login) ??
                       throw new Exception("User not found");

            if (!_hashProvider.Validate(loginModel.Password, user.PasswordHash))
            {
                throw new Exception("Wrong password");
            }

            var jwtTokenContext = new JwtTokenContext
            {
                UserId = user.Id,
                Login  = loginModel.Login,
                Role   = user.RoleId
            };

            return(_tokenProvider.CreateToken(jwtTokenContext));
        }
Esempio n. 3
0
        public TokenModel CreateToken(JwtTokenContext jwtTokenContext)
        {
            var claimsIdentity = GetClaimsIdentity(jwtTokenContext);

            return(CreateToken(claimsIdentity));
        }
 public LoginController(IConfiguration config, JwtTokenContext context)
 {
     _config  = config;
     _context = context;
 }