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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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<string> GenerateToken()
  31. {
  32. var signingCredentials = GetSigningCredentials();
  33. var claims = await GetClaims();
  34. var tokenOptions = GenerateTokenOptions(signingCredentials, claims);
  35. // return created token as string
  36. return new JwtSecurityTokenHandler().WriteToken(tokenOptions);
  37. }
  38. private async Task<List<Claim>> GetClaims()
  39. {
  40. //method creates a list of claims with the user name inside and all the roles the user belongs to.
  41. Claim claim = new (ClaimTypes.Name, customer.UserName);
  42. var claims = new List<Claim>
  43. {
  44. claim
  45. };
  46. IList<string> roles = await _customerManager.GetRolesAsync(customer);
  47. foreach (var role in roles)
  48. {
  49. claims.Add(new Claim(ClaimTypes.Role, role));
  50. }
  51. return claims;
  52. }
  53. private SigningCredentials GetSigningCredentials()
  54. {
  55. // This method returns secret key as a byte array with the security algorithm
  56. var jwtSettings = _configuration.GetSection("JwtSettings");
  57. var key = Encoding.UTF8.GetBytes(jwtSettings["jwtSecret"]);
  58. var secret = new SymmetricSecurityKey(key);
  59. return new SigningCredentials(secret, SecurityAlgorithms.HmacSha256);
  60. }
  61. private JwtSecurityToken GenerateTokenOptions(SigningCredentials
  62. signingCredentials, List<Claim> claims)
  63. {
  64. //return an object of the JwtSecurityToken type with all of the required options
  65. var jwtSettings = _configuration.GetSection("JwtSettings");
  66. var tokenOptions = new JwtSecurityToken
  67. (
  68. issuer: jwtSettings.GetSection("validIssuer").Value,
  69. audience: jwtSettings.GetSection("validAudience").Value,
  70. claims: claims,
  71. expires: DateTime.Now.AddDays(7),
  72. signingCredentials: signingCredentials
  73. );
  74. return tokenOptions;
  75. }
  76. }
  77. }