簡體   English   中英

通過oidc-client登錄IdentityServer4時出現InvalidOperationException

[英]InvalidOperationException when signing in to IdentityServer4 via oidc-client

我在使我的身份驗證流程在帶有IdentityServer4的Vue.js + ASP.NET Core 2.1 Web應用程序之間工作時遇到了一些問題,似乎我缺少了一些非常基本的內容,但不確定是什么。 我想要以下內容:

  1. REST API( http:// localhost:7000 )通過IdentityServer4( http:// localhost:5000 )保護。
  2. 用戶打開我的asp.net核心Web應用程序( http:// localhost:6666 ),單擊登錄
  3. JavaScript提交配置並使用signinredirect將瀏覽器發送到IdentityServer4應用
  4. 用戶輸入登錄詳細信息並提交
  5. 瀏覽器根據成功的登錄詳細信息將每個配置重定向到我的asp.net核心Web應用
  6. 用戶現在可以通過我的asp.net核心網絡應用訪問REST API

我可以從我的vue + Web應用程序-> IdentityServer4 quickstart登錄UI中獲得,它顯示以下登錄UI:

IdentityServer4快速入門UI

我使用測試用戶帳戶“ bob”和“ alice”,在輸入了愛麗絲的用戶標識和密碼並嘗試登錄后,出現以下錯誤:

處理請求時發生未處理的異常。

InvalidOperationException:為方案“ Bearer”注冊的身份驗證處理程序為“ IdentityServerAuthenticationHandler”,不能用於SignInAsync。 已注冊的登錄方案為:idsrv,idsrv.external。

Microsoft.AspNetCore.Authentication.AuthenticationService.SignInAsync(HttpContext上下文,字符串方案,ClaimsPrincipal主體,AuthenticationProperties屬性)

我的客戶端配置為:

new Client
    {
        ClientId = "js",
        ClientName = "JavaScript Client",
        AllowedGrantTypes = GrantTypes.Implicit,
        AllowAccessTokensViaBrowser = true,

        RedirectUris =           { "http://localhost:6666/static/account/callback.html" },
        PostLogoutRedirectUris = { "http://localhost:6666" },
        AllowedCorsOrigins =     { "http://localhost:6666" },

        // scopes that client has access to
        AllowedScopes =
        {
            IdentityServerConstants.StandardScopes.OpenId,
            IdentityServerConstants.StandardScopes.Profile,
            "api1"
        }
    }

我的javascript客戶端的配置為(TypeScript):

// Authentication init code
idConfig: Oidc.UserManagerSettings = {
    authority: "http://localhost:5000",
    client_id: "js",
    redirect_uri: "http://localhost:6666/static/account/callback.html",
    response_type: "id_token token",
    scope: "openid profile castlepoint",
    post_logout_redirect_uri: "http://localhost:6666",
} as Oidc.UserManagerSettings;

而我的登錄JavaScript代碼是:

public login() {
    this.userManager.signinRedirect();
}

我感到我將基於客戶端的登錄流程與自動登錄流程錯誤地結合在一起,但是我不確定...

來自ID4的日志基本上與上述內容相同:

System.InvalidOperationException:為方案“ Bearer”注冊的身份驗證處理程序為“ IdentityServerAuthenticationHandler”,不能用於SignInAsync。 已注冊的登錄方案為:idsrv,idsrv.external。

我的ID4 startup.cs包含以下條目:

public void ConfigureServices(IServiceCollection services)
    {

        // configure identity server with in-memory stores, keys, clients and scopes
        services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryClients(Config.GetClients())
            .AddTestUsers(Config.GetUsers());

        services.AddMvcCore()
           .AddAuthorization()
           .AddJsonFormatters();

        services.AddAuthentication("Bearer")
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = "http://localhost:5000";
                options.RequireHttpsMetadata = false;

                options.ApiName = "api1";
            });

        services.AddCors(options =>
        {
            // this defines a CORS policy called "default"
            options.AddPolicy("default", policy =>
            {
                policy.WithOrigins("http://localhost:6666")
                    .AllowAnyHeader()
                    .AllowAnyMethod();
            });
        });

        services.AddMvc();

}

抱歉,代碼轉儲時間太長...誰能指出我不太正確的容易之處? 我在某些地方與所需的身份驗證類型和所需的配置交叉了...我認為...

好的,我終於知道了。

DI services.AddAuthentication(“ Bearer”)調用將ASP.NET Authentication添加到我的IdentityServer4 Web應用程序中的Startup.cs中,以實現“ Bearer”訪問令牌驗證。 但這不是正確的位置-應該在我的API網絡應用程序中,因為這是API用來驗證對其調用的身份。

IdentityServer4 Web應用程序旨在接受用戶ID +密碼進行身份驗證,但是在我添加的DI中,它還需要Bearer訪問令牌作為身份驗證的一部分。 這基本上弄亂了事情,以及為什么它與登錄方案不兼容。 我有正確的代碼,但在錯誤的應用程序中!

將AddAuthentication(“ Bearer”)移到我的API Web應用程序可以解決整個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM