繁体   English   中英

注销不工作 - Asp.net 核心身份服务器

[英]Logging out not working - Asp.net core identity server

我有一个应用程序,我在其中使用身份服务器进行身份验证。 我那边有些问题。 每当我尝试从系统注销时,系统都不会注销。 即使我已经注销,它也会重定向到主页。 这就是我配置启动类的方式

builder.Services.AddAuthentication(options =>
    {
        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = "oidc";
    })
    .AddCookie(options =>
    {
        options.Cookie.Name = IdentityConstants.ApplicationScheme;
        options.ExpireTimeSpan = TimeSpan.FromMinutes(1);
        options.LogoutPath = "/Home/Logout";
    })
    .AddOpenIdConnect("oidc", options =>
    {
        options.SignInScheme = "Cookies";

        options.Authority = builder.Environment.IsDevelopment() ? appSetting.Development.IdentityServerUrl : appSetting.Production.IdentityServerUrl;
        options.RequireHttpsMetadata = false;

        options.ClientId = "technosys-inv-ui";
        options.ClientSecret = "technosys-inv-secret";
        options.ResponseType = "code id_token";

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

        options.Scope.Add("technosys-inv-api");
        options.ClaimActions.MapJsonKey("website", "website");
    });

    builder.Services.ConfigureApplicationCookie(options =>
    {
        options.Cookie.IsEssential = true;
        options.Cookie.SameSite = SameSiteMode.Unspecified;
    });

这是我的注销操作

[AllowAnonymous]
    public async Task Logout()
    {
        await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme, new AuthenticationProperties
        {
            RedirectUri = "/"
        });
    }

这就是我在身份服务器项目中配置客户端的方式。

public static IEnumerable<Client> GetClients(IConfiguration configuration)
    {
        AppSettings appSettings = configuration.GetSection("AppSettings").Get<AppSettings>();
        AppSetting appSetting = null;
        if (appSettings.Environment == "Development")
            appSetting = appSettings.Development;
        else
            appSetting = appSettings.Production;

        return new[]
        {
            // client credentials flow client
            new Client
            {
                ClientId = "technosys-inv-ui",
                ClientName = "Technosys Inventory UI",
                RedirectUris = { appSetting.AdminClientUrl + "signin-oidc" },
                PostLogoutRedirectUris = { appSetting.AdminClientUrl },
                FrontChannelLogoutUri = appSetting.AdminClientUrl + "signout-oidc",
                AllowedGrantTypes = GrantTypes.Hybrid,
                ClientSecrets = { new Secret("technosys-inv-secret".ToSha256()) },
                AllowOfflineAccess = true,
                AllowedScopes = { "technosys-inv-api", "openid","profile" },
                RequireConsent = false,
            }
        };
    }

这是我的客户端应用程序在此处输入图像描述

如果我单击注销按钮,它会重定向到身份服务器页面并提示,您已注销。 在此处输入图像描述

但是,如果我单击此处标记,它会重定向到客户端主页,而不是在注销后显示登录页面。

注意:注销本地工作但不适用于生产

任何帮助将不胜感激谢谢...!!!

我通常使用这种方法来触发注销:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task Logout()
{
    await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
    await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);

    //Important, this method should never return anything.
}

另外,我建议您应该与身份验证处理程序的命名保持一致,并且不要混合使用您自己的字符串和默认名称。

options.SignInScheme = "Cookies"; vs options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;

AddOpenIdConnect("oidc" vs
AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme)

如果您不小心重命名了字符串值,很容易造成错误。 一致性是这里的关键。

另外,options.LogoutPath = "/Home/Logout"; 需要将确切的 URL 与您的注销页面匹配。

我还会设置 options.Cookie.SameSite = SameSiteMode.Unspecified; 尽可能严格。

使用中间件,您可以检测未经身份验证的用户并发出重定向到登录页面的质询。

您可能还需要检测是否正在进行注销,我脑子里没有这方面的代码,但如果它与您的情况相关,应该将其添加到if条件中。

app.Use(async (context, next) =>
{
    if (!context.User.Identity.IsAuthenticated)
    {
         HttpContext.Authentication.ChallengeAsync( .... );// issue challenge here
    }

    await next(context);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM