blazor - 如何为 Blazor WebAssembly 和 IdentityServer 4 配置持久登录?
问题描述
我们有一个独立的 Blazor WebAssembly 应用程序(PWA),用户需要通过 IdentityServer 4(使用默认设置)进行身份验证(OIDC)。IdentityServer 然后返回一个访问令牌,应用程序使用该令牌调用多个 api。大多数用户在移动设备上使用该应用程序,并且需要在收到通知后使用该应用程序(每小时一次或两次)。一切都运行良好,但由于用户登录不会在白天持续存在,他们需要每天多次重新验证自己。对于用户来说,这真是令人讨厌的行为。因此,我正在寻找一种解决方案,让用户登录至少持续 x 小时。我阅读了几篇博客并尝试更改 IdentityServer 上的设置,但我似乎无法更改此行为。
实际行为:
- 当用户的会话关闭时,用户将被注销并需要在 IdentityServer 上再次登录。
- 大约 1 小时后,用户以任一方式注销。
预期行为:
- 用户的登录在多个会话中持续存在,例如总共 x 小时(我实际上不知道这是否可能?)。
- 只要用户登录并且应用程序处于活动状态(在后台),用户就会保持身份验证。
如果用户只要使用该应用程序就一直保持身份验证,那将有很大帮助。但是,值 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,
},
解决方案
您可以用您的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;
});
推荐阅读
- eclipse - 如何从 jBPM 中的流程图生成代码以在浏览器中进行测试?
- c++ - 从以下代码构造的对象的类型应该是什么,即 new TreeNode
(13) - apache-drill - Apache Drill:阵列的两个横向连接仅使用存储插件失败
- c++ - 犰狳 - 矩阵表达式中没有名为 i 的成员
- java - 我返回了一个数组,但是当我尝试复制该数组时,NetBeans 找不到该符号
- sql-server - SQL Server 2016 变更数据捕获 - 可否用于从源加载仓库暂存表?
- javascript - 无法联系recaptcha。检查您的连接,然后重试。初次提交后(reCaptcha v2 不可见)
- c++ - operator = problem. Why is this happening?
- python - Pandas Dataframe: assigning a new column based comparing each value of one column to all the values of another
- python - def subplot(plt, (Y, X), (sz_y, sz_x) = (10, 10)): throws invalid syntax in Python 3