DbContextAuthenticationService.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using GreenTree.Strohrmann.ERP.Domain.Model;
  2. using GreenTree.Strohrmann.ERP.Services.Authorization;
  3. using Microsoft.AspNetCore.Authentication;
  4. using Microsoft.AspNetCore.Authentication.Cookies;
  5. using Microsoft.AspNetCore.Http;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Security.Claims;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace GreenTree.Strohrmann.ERP.Services.Authentication
  13. {
  14. public class DbContextAuthenticationService : IAuthenticationService
  15. {
  16. #region DI fields
  17. // The global DbContext
  18. private readonly ERPDbContext _eRPDbContext;
  19. // The global HttpContext accessor
  20. private readonly IHttpContextAccessor _httpContextAccessor;
  21. // The global administration options
  22. private readonly AdministrationOptions _administrationOptions;
  23. #endregion
  24. #region Ctor
  25. /// <summary>
  26. /// Initializes a new instance of the DbContextAuthenticationService class
  27. /// </summary>
  28. /// <param name="eRPDbContext">Global DbContext.</param>
  29. /// <param name="httpContextAccessor">Global HTTP context accessor.</param>
  30. /// <param name="administrationOptions">Global administration options.</param>
  31. public DbContextAuthenticationService(
  32. ERPDbContext eRPDbContext,
  33. IHttpContextAccessor httpContextAccessor,
  34. AdministrationOptions administrationOptions)
  35. {
  36. _eRPDbContext = eRPDbContext;
  37. _httpContextAccessor = httpContextAccessor;
  38. _administrationOptions = administrationOptions;
  39. }
  40. #endregion
  41. #region Implementation
  42. /// <summary>
  43. /// Authenticate and sign user in
  44. /// </summary>
  45. /// <param name="httpContext"></param>
  46. /// <param name="username">The username.</param>
  47. /// <param name="isPersistent">The login persistence.</param>
  48. public async void SignIn(string username, bool isPersistent = false)
  49. {
  50. var user = _eRPDbContext.Users
  51. .FirstOrDefault(u => u.Accountname == username || u.MailAddress == username);
  52. if (user == null)
  53. throw new Exception(
  54. String.Format("Der Benutzer \"{0}\" kann nicht gefunden werden.", username));
  55. var claims = new List<Claim>
  56. {
  57. new Claim(ClaimTypes.NameIdentifier, user.Accountname),
  58. new Claim(ClaimTypes.Name, String.Format("{0}, {1}", user.Lastname, user.Forename)),
  59. new Claim(ClaimTypes.Email, user.MailAddress)
  60. };
  61. if (user.Birthdate.HasValue)
  62. claims.Add(new Claim(ClaimTypes.DateOfBirth, user.Birthdate.Value.ToString("dd.MM.yyyy")));
  63. foreach (var policy in user.Policies)
  64. {
  65. claims.Add(new Claim("Policy", policy.PolicyName));
  66. }
  67. var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
  68. var principal = new ClaimsPrincipal(identity);
  69. var authProperties = new AuthenticationProperties
  70. {
  71. AllowRefresh = true,
  72. IsPersistent = isPersistent,
  73. IssuedUtc = DateTimeOffset.UtcNow
  74. };
  75. await _httpContextAccessor.HttpContext.SignInAsync(
  76. CookieAuthenticationDefaults.AuthenticationScheme,
  77. principal,
  78. authProperties);
  79. }
  80. /// <summary>
  81. /// Authenticate and sign user in
  82. /// </summary>
  83. /// <param name="username">The username.</param>
  84. /// <param name="isPersistent">The login persistence.</param>
  85. public async void SignInAdmin(bool isPersistent = false)
  86. {
  87. var claims = new List<Claim>
  88. {
  89. new Claim(ClaimTypes.NameIdentifier, _administrationOptions.Administrator),
  90. new Claim(ClaimTypes.Name, _administrationOptions.Administrator)
  91. };
  92. var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
  93. var principal = new ClaimsPrincipal(identity);
  94. var authProperties = new AuthenticationProperties
  95. {
  96. AllowRefresh = true,
  97. IsPersistent = isPersistent,
  98. IssuedUtc = DateTimeOffset.UtcNow
  99. };
  100. await _httpContextAccessor.HttpContext.SignInAsync(
  101. CookieAuthenticationDefaults.AuthenticationScheme,
  102. principal,
  103. authProperties);
  104. }
  105. /// <summary>
  106. /// Sign user out
  107. /// </summary>
  108. /// <param name="httpContext"></param>
  109. public async void SignOut()
  110. {
  111. await _httpContextAccessor.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
  112. }
  113. #endregion
  114. }
  115. }