簡體   English   中英

使用 OIDC 身份驗證方案注銷 Blazor 應用程序

[英]Sign Out of a Blazor App using the OIDC Authentication Scheme

我們有一個 .NET Core 6 Blazor Server App。 我們使用 OIDC 使用我們自己的身份提供者登錄。 我們在注銷時遇到問題。

我們使用以下代碼塊設置了身份驗證。

builder.Services
    .AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddCookie()
    .AddOpenIdConnect(opts => {
        opts.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        opts.RequireHttpsMetadata = !isDebug;
        opts.ClientId = "user-accounts-app";
        opts.CallbackPath = "/signin-oidc";
        opts.ResponseType = OpenIdConnectResponseType.Code;
        opts.Authority = authority;
        opts.ClientSecret = builder.Configuration["CLIENT_SECRET"];
        var scopes = new List<string>() {
            "openid", "profile", "email", "phone", "offline_access"
        };
        foreach(var s in scopes)
        {
            opts.Scope.Add(s);
        }
    });

發現文檔確實包含一個end_session_endpoint 但是,端點永遠不會被擊中。 我們嘗試從 razor 頁面注銷

await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
// This line does not work
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties
{
    RedirectUri = "http://mydomainhere.com/our/path/here",
});

運行第二個SignOutAsync似乎什么都不做。 在 session 端點末尾未命中身份提供者,我們的注銷頁面上沒有任何反應。 我們的 session 沒有從 IDP 中清除。

此外,我們的 blazor 應用程序的 cookies 並未完全清除。 我們有大量揮之不去的.AspNetCorrelation.hash<hash-here>路徑/signin-oidc (試圖獲取屏幕截圖,但現在服務器出現錯誤)。 但是第一次SignOutAsync調用成功清除了 .AspNetCore cookie。

我不確定第二個 SignOutAsync 的行為應該是什么。 它會將用戶重定向到 IDP 的注銷 url 嗎? 還是它在后台執行此操作? 我們是否在調用AddOpenIdConnect()以處理注銷時缺少某些配置?

看起來我們只是缺少 OIDC 注銷方案。

opts.SignOutScheme = OpenIdConnectDefaults.AuthenticationScheme;

這就是我們讓它工作所需的一切。

如果沒有指定退出方案,ASP.net 將使用登錄方案。 登錄方案是 cookie,這有點誤導,因為 OpenID 機構實際上是您登錄的機構。 登錄會導致創建 cookie 以存儲該機構提供的身份驗證令牌(因此您可以有效地登錄到客戶端應用程序)。 如果您使用 cookie 方案注銷,則只會銷毀 cookie——您將退出客戶端,但不會退出權限。 下次你來到一個頁面時,你只會得到一個新的 cookie,因為你已經登錄了授權。

因此,上面的注銷方案注銷了權限,而不僅僅是客戶端。 我不確定弄清楚的同事是否也添加了刪除cookie的步驟。 如果我發現他們這樣做了,我會用細節來編輯它。 它可能以某種方式被 asp 框架神奇地處理。

暫無
暫無

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

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