簡體   English   中英

身份服務器 4:從 MVC 客戶端正確注銷

[英]Identity Server 4 : Proper logout from MVC Client

我在 IdentityServer 4 中的注銷功能遇到了麻煩。我的 IS4 應用程序主要是他們網站上教程的結果,所以他們並不是真正的自定義行為。 我也使用 ASP.net Core Identity。 我有一個 MVC 客戶端(同樣,基本上是項目模板)。 我剛剛在索引頁面的頂部添加了一個“注銷”按鈕,以便將當前經過身份驗證的用戶注銷。

這是我的 MVC 客戶端中的注銷方法:

public async Task Logout()
{
    await HttpContext.SignOutAsync("Cookies");
    await HttpContext.SignOutAsync("oidc");
}

這正是教程所說的。

這是 MVC Client 的 Startup.cs 中的配置:

services.AddAuthentication(options =>
{
    options.DefaultScheme = "Cookies";
    options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
    options.SignInScheme = "Cookies";

    options.Authority = "http://localhost:5000";
    options.RequireHttpsMetadata = false;
    options.CallbackPath = new PathString("/Home/");

    options.ClientId = "Core.WebUI";
    options.ClientSecret = "secret";
    options.ResponseType = "code id_token";

    options.SaveTokens = true;
    options.GetClaimsFromUserInfoEndpoint = true;

    options.Scope.Add("offline_access");                    
});

沒什么特別的……現在 IS4 應用程序中的 MVC 客戶端配置:

new Client
{
    ClientId = "Core.WebUI",
    ClientName = "MVC Client",
    ClientSecrets = new List<Secret>
    {
        new Secret("secret".Sha256())
    },
    AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
    RequireConsent = false,

    // where to redirect to after login
    RedirectUris = { "http://localhost:5011/Home/" },

    // where to redirect to after logout
    PostLogoutRedirectUris = { "http://localhost:5011/Home/" },
    AlwaysSendClientClaims = true,
    AlwaysIncludeUserClaimsInIdToken = true,
    AllowedScopes =
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile
    },
    AllowOfflineAccess = true
}

同樣,主要是教程所說的。 我的問題是:當用戶連接時,然后我單擊注銷按鈕,我被重定向到 IS4 應用程序,在注銷頁面中,說我現在已注銷。 但實際上,我不是,因為如果我回到我的 MVC,我仍然可以訪問受保護的功能(使用 Authorize 屬性)。 為了正確注銷我的用戶,一旦我進入我的 D4 應用程序的注銷頁面,我必須點擊 IS4 應用程序的注銷按鈕......然后我才能正確注銷......

我想要的是,當我單擊 MVC 客戶端上的“注銷”按鈕時,我真的會注銷,並直接重定向到我的 MVC 客戶端的主頁(沒有“您現在已注銷”頁面)

我對 IS4 和 ADP.NET 還很陌生,所以非常歡迎任何幫助......謝謝!

這是我解決這個問題的方法:

    public IActionResult LogOff()
    {
        return new SignOutResult(new[] { "oidc", "Cookies" });
    }

最好不要使用魔法字符串,但是:

  return new SignOutResult(new[]
            {
                CookieAuthenticationDefaults.AuthenticationScheme, 
                OpenIdConnectDefaults.AuthenticationScheme
            });

你有沒有試過,

public async Task<IActionResult> Logout()
{
   await _signInManager.SignOutAsync();
   return View("Logout"); // or whatever url Redirect("http://localhost:5011/Home/")
}

暫無
暫無

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

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