Фабрика:
Код: Выделить всё
using System.Security.Claims;
using Domain.Identity;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Shared.Authorization;
namespace Infrastructure.Identity
{
public class AppClaimsPrincipalFactory : UserClaimsPrincipalFactory
{
public AppClaimsPrincipalFactory(
UserManager userManager,
IOptions options) : base(userManager, options)
{
}
public override async Task CreateAsync(ApplicationUser user)
{
var principal = await base.CreateAsync(user);
var identity = (ClaimsIdentity)principal.Identity!;
// Add custom claims
identity.AddClaim(new Claim(AppClaims.CompanyId, user.CompanyId.ToString()));
return principal;
}
protected override async Task GenerateClaimsAsync(ApplicationUser user)
{
var identity = await base.GenerateClaimsAsync(user);
identity.AddClaim(new Claim(AppClaims.CompanyId, user.CompanyId.ToString()));
return identity;
}
}}
Ниже показано, как я пытаюсь это зарегистрировать:
Код: Выделить всё
using Domain.Identity;
using Infrastructure.Persistence.Context;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
namespace Infrastructure.Identity;
internal static class Startup
{
internal static IServiceCollection AddIdentity(this IServiceCollection services)
{
services
.AddIdentity(options =>
{
options.Password.RequiredLength = 6;
options.Password.RequireDigit = false;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = true;
options.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores()
.AddClaimsPrincipalFactory()
.AddDefaultTokenProviders();
services.AddTransient();
return services;
}
}
Код: Выделить всё
services.AddScoped();
Код: Выделить всё
.AddClaimsPrincipalFactory()
помогите!
Подробнее здесь: https://stackoverflow.com/questions/785 ... sed-even-t
Мобильная версия