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

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