skip to Main Content

I have the boilerplate Identity pages for User creation/login etc. All works fine, Users are stored in my MSSQL backend. I have 2 pages in my project that are both behind an [Authorize] tag.

The first page you are redirected to after successful log in and it works without an issue. When you click a button on that page to go to the next page, you’re asked to sign in again. Once logged in for the second time that page displays correctly. I’ve gone around and around the documentation trying to figure out why the user can’t "presist" its log in status between pages but my knowledge of these systems is very limited and the documentations are often written for people with a lot more technical experience than I have.

Does anyone have any idea what the problem could be? My Program.cs setup relevant to Identity is here:

builder.Services.AddDefaultIdentity<User>(options => options.SignIn.RequireConfirmedAccount = true)
      .AddEntityFrameworkStores<MyDatabaseContext>()
      .AddDefaultTokenProviders();

builder.Services.ConfigureApplicationCookie(options =>
{
    options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
    options.LoginPath = "/Identity/Account/Login";
    options.SlidingExpiration = true;
});

// Add logger services
builder.Host.UseSerilog((context, services, configuration) => configuration
    .ReadFrom.Services(services)
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)); 

builder.Services.AddDistributedMemoryCache();

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    // app.UseHsts();
}

app.UseSerilogRequestLogging(); 
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.MapRazorPages();
app.MapControllers();

This post sounds very similar to my issue, but I don’t see what their solution is suggesting that I haven’t done? Asp.net core Identity successful login redirecting back to login page

2

Answers


  1. Chosen as BEST ANSWER

    I figured out the problem, it was not code related. Changing the IIS pool value from to Classical on my hosting platform solved it.


  2. You need to use AddAuthentication middleware add after yConfigureApplicationCookie configurations.

     builder.Services.ConfigureApplicationCookie(options =>
                {
                    options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
                    options.LoginPath = "/Identity/Account/Login";
                    options.SlidingExpiration = true;
                });
                builder.Services.AddAuthentication("CookieAuth")
                   .AddCookie("CookieAuth", options =>
                   {
                       options.Cookie.Name = "CookieAuth";
                       options.LoginPath = "/Account/Login";
                       options.LogoutPath = "/Account/Logout";
                   });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search