简体   繁体   中英

How to use 2 cookies for auth?

I have 2 devices, pc and a special tablet. I want to have with same app .net core 2.0 2 cookies, or cookies scheme with 2 auth cookies, because for pc I want to expire in 5 minutes and for that special tablet to not expire at all. How to do that. Now I have this...

app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationScheme = "myScheme",
                ExpireTimeSpan = TimeSpan.FromSeconds(300),
                CookiePath = "/",
                CookieSecure = env.IsDevelopment() ? CookieSecurePolicy.SameAsRequest : CookieSecurePolicy.Always
            });

I think I could use UseWhen method or I don't know...

Or to sign in on different cookies?

Check Using cookie authentication without ASP.NET Core Identity

public void ConfigureServices(IServiceCollection services)
{
        services.AddAuthentication(options =>
                {
                    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                })
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
                    {
                        options.LoginPath = "/auth";
                        //https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.cookiebuilder?view=aspnetcore-2.1
                        options.Cookie = new CookieBuilder
                        {
                            Name = "CustomCookie",
                            HttpOnly = false
                        };
                    });
}

public async void Configure(IApplicationBuilder app)
{
     app.UseAuthentication();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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