首页 > 解决方案 > 如何为 Blazor WebAssembly 和 IdentityServer 4 配置持久登录?

问题描述

我们有一个独立的 Blazor WebAssembly 应用程序(PWA),用户需要通过 IdentityServer 4(使用默认设置)进行身份验证(OIDC)。IdentityServer 然后返回一个访问令牌,应用程序使用该令牌调用多个 api。大多数用户在移动设备上使用该应用程序,并且需要在收到通知后使用该应用程序(每小时一次或两次)。一切都运行良好,但由于用户登录不会在白天持续存在,他们需要每天多次重新验证自己。对于用户来说,这真是令人讨厌的行为。因此,我正在寻找一种解决方案,让用户登录至少持续 x 小时。我阅读了几篇博客并尝试更改 IdentityServer 上的设置,但我似乎无法更改此行为。

实际行为:

预期行为:

如果用户只要使用该应用程序就一直保持身份验证,那将有很大帮助。但是,值 1 小时的唯一设置(我可以在 IdentityServer 中找到)是访问令牌生命周期,默认为 3600 秒。不过,我还读到,这与我的问题无关,即用户必须重新验证自己。

我想我忽略了一些东西,但我不知道如何解决这个问题。任何帮助表示赞赏!

更新

在我的 Blazor WebAssembly 项目中,我有以下配置Program.cs

      builder.Services.AddOidcAuthentication(options =>
            {
                builder.Configuration.Bind("IdentityService", options.ProviderOptions);
            });

And in appsettings.json:

    "IdentityService": {
        "Authority": "https://localhost:44362",
        "ClientId": "MyBlazorClient",
        "DefaultScopes": [
            "openid",
            "profile",
            "claims",
            "roles"
        ],
        "ResponseType": "code"
    },

在 IdentityServer 中,我在我的StartUp.cs

    var builder = services.AddIdentityServer(options =>
            {
                options.Events.RaiseErrorEvents = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents = true;
                options.Events.RaiseSuccessEvents = true;
                options.UserInteraction.LoginUrl = "/Account/Login";
                options.UserInteraction.LogoutUrl = "/Account/Logout";
                options.Authentication = new AuthenticationOptions()
                {
                    CookieLifetime = TimeSpan.FromHours(8), 
                    CookieSlidingExpiration = true
                };
            })

最后,这是我在 IdentityServer 中的客户端配置:

    new Client
    {
        ClientId = "MyBlazorClient",
        ClientName = "My Blazor Client Application",
        RequireClientSecret = false,
        AllowedCorsOrigins = { https://localhost:443 },
        AllowedGrantTypes = GrantTypes.Code,
        RedirectUris = { "https://localhost:443/authentication/login-callback" },
        PostLogoutRedirectUris = { "https://localhost:443/authentication/logout-callback" },
        AllowedScopes = new List<string>
        {
            IdentityServerConstants.StandardScopes.OpenId,
            IdentityServerConstants.StandardScopes.Profile,
            "roles",
            "claims"
        },
        RequirePkce = true,
        AllowPlainTextPkce = false,
    },

标签: blazoridentityserver4progressive-web-appsopenid-connectblazor-webassembly

解决方案


您可以用您的Startup.cs文件和所有身份/身份验证配置更新问题吗?

您可能没有设置 Identity Server 将接受的应用程序 Cookie。这是一个例子:

services.ConfigureApplicationCookie(options =>
{
    options.Cookie.Name = "MyAppCookie";
    options.Cookie.HttpOnly = true;
    options.Cookie.SameSite = SameSiteMode.Strict;
    options.ExpireTimeSpan = TimeSpan.FromDays(1);
    options.Cookie.MaxAge = TimeSpan.FromDays(7);
    options.SlidingExpiration = true;
    options.LoginPath = "/login";
    options.LogoutPath = "/logout";
    options.AccessDeniedPath = "/error";
    options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
});

推荐阅读