繁体   English   中英

IdentityServer4 无法使用自定义用户存储添加 asp net core 身份

[英]IdentityServer4 can't add asp net core identity with custom user store

我正在尝试将我的自定义 IUserStore 实现与 IdentityServer4 + Asp Net Core Identity 一起使用,我遵循的步骤是在删除 EntityFramework 资产后创建新的“IdentityServer with Asp Net Core Identity”又名 is4aspid 模板,然后我的配置服务看起来像

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();

        services.AddScoped<IIdentityUserRepository<ApplicationUser>, IdentityUserRepository>(); //<-- Repository that I used on CustomUserStore

        services.AddDefaultIdentity<ApplicationUser>()
            .AddUserStore<CustomUserStore<ApplicationUser>>() //<-- Add
            .AddDefaultTokenProviders();

        var builder = services.AddIdentityServer(options =>
            {
                options.Events.RaiseErrorEvents = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents = true;
                options.Events.RaiseSuccessEvents = true;

                // see https://identityserver4.readthedocs.io/en/latest/topics/resources.html
                options.EmitStaticAudienceClaim = true;
            })
            .AddInMemoryIdentityResources(Config.IdentityResources)
            .AddInMemoryApiScopes(Config.ApiScopes)
            .AddInMemoryClients(Config.Clients)
            .AddAspNetIdentity<ApplicationUser>();
    }

自定义用户商店看起来像

public class CustomUserStore<TUser> :
    IUserStore<TUser>,
    IUserLoginStore<TUser>,
    IUserRoleStore<TUser>,
    IUserClaimStore<TUser>,
    IUserPasswordStore<TUser>,
    IUserSecurityStampStore<TUser>,
    IUserEmailStore<TUser>,
    IUserLockoutStore<TUser>,
    IUserPhoneNumberStore<TUser>
    where TUser : ApplicationUser
{
    private readonly IIdentityUserRepository<TUser> _userRepository;

    public CustomUserStore(IIdentityUserRepository<TUser> userRepository)
    {
        _userRepository = userRepository;
    } //rest of the code hidden sake of brevity

自定义用户存储适用于默认的 Asp Net Core 标识模板,但在 is4aspid 模板中,当我尝试摆脱 Entity Framework 并放置我的自定义存储实现时,登录页面在我尝试访问受保护资源时返回 404 消息,但除此之外,主页页面正常工作,没有错误消息或下面的日志消息

[13:03:04 Information] Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler
AuthenticationScheme: Identity.Application was challenged.

此外,当这些事情发生时,没有调用控制器或 CustomUserStore

我使用的文档

ASP.NET 核心标识的自定义存储提供程序

IdentityServer4 使用 ASP.NET 核心身份

编辑:ApplicationUser class 也是自定义实现,与默认ApplicationUser: IdentityUser带有模板本身

我使用这样的代码将我自己的商店添加到 IdentityServer:

services.AddDbContext<ApplicationDbContext>(options =>
{
    options.UseSqlServer(_configuration["ConnectionString"]);
});

services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

问题是AddDefaultIdentity本身,因为它不仅添加了 Identity 组件,还添加了很多东西,包括 UI,我认为问题是因为 UI 组件与AddDefaultIdentity一起添加的,所以当我尝试使用项目的视图时它混淆了框架,解决方案是使用AddIdentity而不是AddDefaultIdentity这样就解决了问题,现在系统运行顺畅。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM