Example #1
0
 public PrincipalReadDTO AuthenticatePrincipal(PrincipalCreateDTO principal)
 {
     try
     {
         var prin = userService.UserWithCredentialsExists(principal.Email, principal.Password);
         return(prin);
     }
     catch (Exception)
     {
         throw new Exception("not found");
     }
 }
 public IActionResult Authenticate(PrincipalCreateDTO principal)
 {
     try
     {
         var role = authenticationHelper.AuthenticatePrincipal(principal);
         principal.Role = role.Role;
         // principal.Role = role;
         var tokenString = authenticationHelper.GenerateJwt(principal);
         return(Ok(new { token = tokenString, role = role }));
     }
     catch (Exception e) {
         return(Unauthorized());
     }
 }
Example #3
0
        public string GenerateJwt(PrincipalCreateDTO principal)
        {
            var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Jwt:Key"]));
            var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);


            var claims = new[]
            {
                new Claim(ClaimTypes.Role, principal.Role)
            };

            var token = new JwtSecurityToken(configuration["Jwt:Issuer"],
                                             configuration["Jwt:Issuer"],
                                             claims: claims,
                                             expires: DateTime.Now.AddMinutes(120),
                                             signingCredentials: credentials);


            return(new JwtSecurityTokenHandler().WriteToken(token));
        }