ASP.NET核心身份到期(Google OAuth)



我当前正在使用asp.net核心身份。我无法找出扩展会话长度的设置,但是我一直登录 - 我认为滑动到期约20分钟,但我找不到设置。请注意,我将Google用作外部Oauth。

        services.AddIdentity<ApplicationUser, IdentityRole>(o =>
            {
                o.Password.RequireDigit = false;
                o.Password.RequireLowercase = false;
                o.Password.RequireUppercase = false;
                o.Password.RequireNonAlphanumeric = false;
                o.Password.RequiredLength = 6;
                o.SecurityStampValidationInterval = TimeSpan.FromHours(8);
                o.Cookies.ExternalCookie.ExpireTimeSpan = TimeSpan.FromHours(8);
                o.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromHours(8);
            })
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        app.UseIdentityServer();
        app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
        {
            Authority = $"http://localhost:55504/",
            RequireHttpsMetadata = false,
            AllowedScopes =
            {
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile,
                IdentityServerConstants.StandardScopes.Email,
                "name",
                "given_name",
                "family_name",
                "role"
            }
        });
        var googleOptions = serviceProvider.GetRequiredService<GoogleOptions>();
        app.UseGoogleAuthentication(new GoogleOptions
        {
            AuthenticationScheme = "Google",
            SignInScheme = "Identity.External",
            ClientId = googleOptions.ClientId,
            ClientSecret = googleOptions.ClientSecret
        });

这个问题答案特定于身份服务器4。

您将在配置中执行类似的操作:

app.UseGoogleAuthentication(new GoogleOptions
{
    SignInScheme = "Identity.External", // this is the name of the cookie middleware registered by UseIdentity()
    ClientId = Configuration["ExternalAuthentication:Google:ClientId"],
    ClientSecret = Configuration["ExternalAuthentication:Google:ClientSecret"]
});
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
    Authority = $"http://localhost:55504/",
    RequireHttpsMetadata = false,
    AllowedScopes =
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        IdentityServerConstants.StandardScopes.Email,
        "name",
        "given_name",
        "family_name",
        "role"
    }
        // CookieLifetime default is 10 Hours
        Authentication.CookieLifetime = TimeSpan.FromHours(24);
        // Default CookieSlidingExpiration = false;
        Authentication.CookieSlidingExpiration = true;   
});

和在您的配置中

    // Identity
    // https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity
    // http://docs.identityserver.io/en/release/quickstarts/6_aspnet_identity.html
    services.AddIdentity<ApplicationUser, IdentityRole>(o => {
            // configure identity options
            o.Password.RequireDigit = false;
            o.Password.RequireLowercase = false;
            o.Password.RequireUppercase = false;
            o.Password.RequireNonAlphanumeric = false;
            o.Password.RequiredLength = 6;
        })
            .AddEntityFrameworkStores<AuthDbContext>()
            .AddDefaultTokenProviders();

相关内容

  • 没有找到相关文章

最新更新