How do I stop an ASP.Net web page from signing in the previous user automatically?

I am working my way through an old Udemy course on ASP.Net. The course author hasn’t replied to questions in two years.

The course has us use the following Configure() method in Startup.cs:

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        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.UseHttpsRedirection();
    app.UseStaticFiles();

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

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}

Before the UseAuthentication() line was added, buttons labeled “Register” and “Log In” appeared on the top line of the web page. After that line was added, they disappeared. The reason was that _LoginPartial.cshtml checks to see if a user is already logged in, and does not create the buttons if there is one:

@inject Microsoft.AspNetCore.Identity.SignInManager<AppointmentScheduling.Models.ApplicationUser> signInManager

@if(signInManager.IsSignedIn(User))
{
    
}
else
{
    <ul class="nav navbar-nav">
        <li class="nav-item">
            @*@Html.ActionLink("Sign Up", "Register", "Account", routeValues:null, htmlAttributes: new { id = "registerLink" } )*@
            <a class="nav-link text-white" id="registerLink" asp-controller="Account" asp-action="Register">Register</a>
*       </li>
        <li>
            @*@Html.ActionLink("Sign In", "Login", "Account", routeValues:null, htmlAttributes: new { id = "registerLink" } )*@
            <a class="nav-link text-white" id="registerLink" asp-controller="Account" asp-action="Login">Sign In</a>
        </li>
    </ul>
}

I added some code in the if block to verify that a user is signed in even though the web site has been started and nobody has signed in yet.

What do I have to do to get my web page to stop automatically assuming that the current person using the page is the same as the last person who used it?

Leave a Comment