简体   繁体   中英

Redirect to login page after being already logged in identity .net 6

I use ASP.NET-Core Identity (6.0.8) and .NET 6. When I login the website, everything is fine, But when I want to go to the user panel page. it redirects me to the login page again. Even the claims are stored in the database

builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ExampleContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("example"));
});


builder.Services.AddAuthorization();
builder.Services.AddAuthentication();


builder.Services.AddIdentity<User, IdentityRole>(options =>
{
    options.User.RequireUniqueEmail = false;
    options.Tokens.ChangePhoneNumberTokenProvider = "12";
    options.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultPhoneProvider;
    options.Password.RequireUppercase = true;
    options.Password.RequireDigit = true;
    options.Password.RequiredLength = 6;
    options.Password.RequireLowercase = false;
    options.Password.RequiredUniqueChars = 2;
    options.Password.RequireNonAlphanumeric = true;
    options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(15);
}).AddEntityFrameworkStores<ExampleContext>().AddDefaultTokenProviders();

builder.Services.ConfigureApplicationCookie(options =>
{
    // Cookie settings
    options.Cookie.HttpOnly = true;
    options.ExpireTimeSpan = TimeSpan.FromMinutes(5);

    options.LoginPath = "/Login";
    options.AccessDeniedPath = "/AccessDenied";
    options.SlidingExpiration = true;
});
builder.Services.AddRazorPages();


app.UseAuthorization();
app.UseAuthentication();
app.MapRazorPages();

options.ExpireTimeSpan = TimeSpan.FromMinutes(5);

Because your cookie ExpireTimeSpan is 5 minutes, so after 5 minutes you will need login again.

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