微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我正在尝试将我的自定义 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 模板中,当我尝试摆脱实体框架并放置自定义存储实现时,登录页面在我尝试访问受保护资源时返回 404 消息,但除了即,主页工作正常,下面没有错误信息或登录信息

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

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

我使用的文档

Custom storage providers for ASP.NET Core Identity

IdentityServer4 Using ASP.NET Core Identity

编辑:ApplicationUser 类也是自定义实现,没有任何继承者,不像ApplicationUser : IdentityUser 自带模板

解决方法

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

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。