为什么用户关闭浏览器后就退出了?

问题描述 投票:0回答:2

我使用 Identity Server 和 .Net 6。配置身份验证:

services
    .AddAuthentication(options =>
    {
        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultAuthenticateScheme = OpenIdConnectDefaults.AuthenticationScheme;
    })
    .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
    {
        options.ReturnUrlParameter = "requestedUrl";
    })
    .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
    {
        options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.Authority = "IdentityServer";
        options.ResponseType = OidcConstants.ResponseTypes.CodeIdToken;
        options.ClientId = "IdentityClientId";
        options.ClientSecret = "IdentityClientSecret";
        options.RequireHttpsMetadata = false;
        options.SignedOutRedirectUri = websiteSettings.Home;
        options.GetClaimsFromUserInfoEndpoint = true;
        options.Scope.Add("ordering-api");
        options.Scope.Add("offline_access");
        options.SaveTokens = true;
        options.RemoteAuthenticationTimeout = TimeSpan.FromMinutes(30);
        options.TokenValidationParameters.RoleClaimType = "role";
        options.Events = new OpenIdConnectEvents
        {
            // .. some events
        };
    });

我想要的是,当我向我的网络应用程序验证用户身份时,他们可以关闭浏览器,当他们重新打开浏览器时,他们仍然可以使用我的网站 - 他们还没有注销。

当我打开浏览器时,我没有看到 IDS cookie。

identityserver4 .net-6.0
2个回答
1
投票

您必须在控制器方法(POST:登录)中指示您希望持久登录的用户登录位置。

await _signInManager.SignInAsync(<ApplicationUser>, isPersistent: <boolean>);

这里必须传递第二个参数

true
。默认为
false

参考:https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.signinmanager-1.signinasync?view=aspnetcore-7.0#microsoft-aspnetcore-identity-signinmanager-1- Signinasync(-0-系统布尔值系统字符串)


0
投票

我添加了选项:

services.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
            {
                options.SlidingExpiration = true;
                options.ReturnUrlParameter = "requestedUrl";

                options.Cookie.SameSite = SameSiteMode.Lax;
                options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;

                options.Cookie.IsEssential = true;
                options.Cookie.MaxAge = TimeSpan.FromDays(AuthOptions.CookiesLifetime);
            });

并添加:

await HttpContext.SignInAsync(new IdentityServerUser(user.UserId.ToString())
        {
            DisplayName = user.Email,
            AdditionalClaims = new List<Claim> { new(JwtClaimTypes.Email, user.Email) }
        },
        new AuthenticationProperties()
        {
            IsPersistent = true,
            ExpiresUtc = DateTime.UtcNow.AddDays(AuthOptions.CookiesLifetime),
            AllowRefresh = true
        });
© www.soinside.com 2019 - 2024. All rights reserved.