You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AuthenticationService.cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Diligent.WebAPI.Business.Interfaces;
  2. using Diligent.WebAPI.Data.Entities;
  3. using Microsoft.AspNetCore.Identity;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.IdentityModel.Tokens;
  6. using System.IdentityModel.Tokens.Jwt;
  7. using System.Security.Claims;
  8. using System.Text;
  9. namespace Diligent.WebAPI.Business.Services
  10. {
  11. public class AuthenticationService : IAuthenticationService
  12. {
  13. private readonly UserManager<Customer> _customerManager;
  14. private readonly IConfiguration _configuration;
  15. private Customer _customer;
  16. public AuthenticationService(UserManager<Customer> customerManager, IConfiguration configuration)
  17. {
  18. _customerManager = customerManager;
  19. _configuration = configuration;
  20. }
  21. public async Task<bool> ValidateCustomer(string username, string password)
  22. {
  23. _customer = await _customerManager.FindByNameAsync(username);
  24. return (_customer != null && await _customerManager.CheckPasswordAsync
  25. (_customer, password));
  26. }
  27. public async Task<string?> GenerateToken()
  28. {
  29. if (_customer == null) return null;
  30. var signingCredentials = GetSigningCredentials();
  31. var claims = await GetClaims();
  32. var tokenOptions = GenerateTokenOptions(signingCredentials, claims);
  33. // return created token as string
  34. return new JwtSecurityTokenHandler().WriteToken(tokenOptions);
  35. }
  36. private async Task<List<Claim>> GetClaims()
  37. {
  38. //method creates a list of claims with the user name inside and all the roles the user belongs to.
  39. Claim claim = new(ClaimTypes.Name, _customer.UserName);
  40. var claims = new List<Claim>
  41. {
  42. claim
  43. };
  44. IList<string> roles = await _customerManager.GetRolesAsync(_customer);
  45. foreach (var role in roles)
  46. {
  47. claims.Add(new Claim(ClaimTypes.Role, role));
  48. }
  49. return claims;
  50. }
  51. private SigningCredentials GetSigningCredentials()
  52. {
  53. // This method returns secret key as a byte array with the security algorithm
  54. var jwtSettings = _configuration.GetSection("JwtSettings");
  55. var key = Encoding.UTF8.GetBytes(jwtSettings["jwtSecret"]);
  56. var secret = new SymmetricSecurityKey(key);
  57. return new SigningCredentials(secret, SecurityAlgorithms.HmacSha256);
  58. }
  59. public async Task<Customer> GetByUserName(string username)
  60. {
  61. return await _customerManager.FindByNameAsync(username);
  62. }
  63. private JwtSecurityToken GenerateTokenOptions(SigningCredentials
  64. signingCredentials, List<Claim> claims)
  65. {
  66. //return an object of the JwtSecurityToken type with all of the required options
  67. var jwtSettings = _configuration.GetSection("JwtSettings");
  68. var tokenOptions = new JwtSecurityToken
  69. (
  70. issuer: jwtSettings.GetSection("validIssuer").Value,
  71. audience: jwtSettings.GetSection("validAudience").Value,
  72. claims: claims,
  73. expires: DateTime.Now.AddDays(7),
  74. signingCredentials: signingCredentials
  75. );
  76. return tokenOptions;
  77. }
  78. }
  79. }