The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
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.

TeknikProfileService.cs 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using IdentityModel;
  2. using IdentityServer4.Extensions;
  3. using IdentityServer4.Models;
  4. using IdentityServer4.Services;
  5. using Microsoft.AspNetCore.Identity;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Security.Claims;
  10. using System.Threading.Tasks;
  11. using Teknik.IdentityServer.Models;
  12. using Teknik.Utilities;
  13. namespace Teknik.IdentityServer
  14. {
  15. public class TeknikProfileService : IProfileService
  16. {
  17. private readonly IUserClaimsPrincipalFactory<ApplicationUser> _claimsFactory;
  18. private readonly UserManager<ApplicationUser> _userManager;
  19. public TeknikProfileService(UserManager<ApplicationUser> userManager, IUserClaimsPrincipalFactory<ApplicationUser> claimsFactory)
  20. {
  21. _userManager = userManager;
  22. _claimsFactory = claimsFactory;
  23. }
  24. public async Task GetProfileDataAsync(ProfileDataRequestContext context)
  25. {
  26. var sub = context.Subject.GetSubjectId();
  27. var user = await _userManager.FindByIdAsync(sub);
  28. var principal = await _claimsFactory.CreateAsync(user);
  29. var claims = principal.Claims.ToList();
  30. claims.AddRange(user.ToClaims());
  31. claims = claims.Where(claim => context.RequestedClaimTypes.Contains(claim.Type)).ToList();
  32. context.IssuedClaims = claims;
  33. }
  34. public async Task IsActiveAsync(IsActiveContext context)
  35. {
  36. var sub = context.Subject.GetSubjectId();
  37. var user = await _userManager.FindByIdAsync(sub);
  38. context.IsActive = user != null && user.AccountStatus == Utilities.AccountStatus.Active;
  39. }
  40. }
  41. }