簡體   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