hanyadikhetvan-dotnethf/HanyadikHetVan/Infrastructure/ProfileService.cs

44 lines
1.5 KiB
C#
Raw Normal View History

2021-05-19 17:56:56 +02:00
using HanyadikHetVan.Data.Entities;
using IdentityServer4.Models;
using IdentityServer4.Services;
using Microsoft.AspNetCore.Identity;
2021-05-19 23:14:57 +02:00
using Microsoft.Extensions.Logging;
using System;
2021-05-19 17:56:56 +02:00
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace HanyadikHetVan.Infrastructure
{
public class ProfileService : IProfileService
{
private readonly UserManager<User> userManager;
2021-05-19 23:14:57 +02:00
private readonly ILogger<ProfileService> _logger;
2021-05-19 17:56:56 +02:00
2021-05-19 23:14:57 +02:00
public ProfileService(UserManager<User> userManager, ILogger<ProfileService> logger)
2021-05-19 17:56:56 +02:00
{
this.userManager = userManager;
2021-05-19 23:14:57 +02:00
this._logger = logger;
2021-05-19 17:56:56 +02:00
}
public async Task GetProfileDataAsync(ProfileDataRequestContext context)
{
var user = await userManager.GetUserAsync(context.Subject);
var roles = await userManager.GetRolesAsync(user);
if (context.RequestedClaimTypes.Any(x => x == "user_role"))
{
context.IssuedClaims.AddRange(roles.Select(x => new Claim("user_role", x)));
}
if (context.RequestedResources.ParsedScopes.Any(x => x.ParsedName == "profile"))
{
context.IssuedClaims.Add(new Claim("email", user.Email));
}
}
public async Task IsActiveAsync(IsActiveContext context)
{
var user = await userManager.GetUserAsync(context.Subject);
context.IsActive = user != null;
}
}
}