Search code examples
asp.net-corerazor-pagesasp.net-core-identity

Sign-in user will never see the Login page again unless Logout. Asp.net core (2.1 or 2.2 or 3.0)


I want to fix an issue where the sign-in user will never see the login page again unless he/she logout. How can i do it in asp.net core 2.2 identity razor page???


Solution

  • I want to fix an issue where the sign-in user will never see the login page again unless he/she logout. How can i do it in asp.net core 2.2 identity razor page???

    To achieve above requirement, you can create and use a custom Razor Page filter to check request path and current user login status, like blow.

    public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context)
    {
        if (context.HttpContext.Request.Path == "/Identity/Account/Login"&& context.HttpContext.User.Identity.IsAuthenticated)
        {
            //redirect to index page
            context.HttpContext.Response.Redirect("/");
        }
    
        return Task.CompletedTask;
    }