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.4KB

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