Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 即使在登录后也能进入登录页面_Asp.net Core - Fatal编程技术网

Asp.net core 即使在登录后也能进入登录页面

Asp.net core 即使在登录后也能进入登录页面,asp.net-core,Asp.net Core,一、 m使用默认的模型视图控制器模板并启用个人用户身份验证。问题是当我登录后,我仍然可以进入登录页面。如何防止它?您可以自定义一个中间件,以便在用户登录并访问登录页面时重定向 重定向中间件.cs Startup.cs 我实现了你的答案。问题是RedirectMiddleware类中的IsAuthenticated方法即使在经过身份验证后仍抛出false。@PrabeshUpreti确保app.useMidleware;是afterapp.UseAuthentication; public cla

一、 m使用默认的模型视图控制器模板并启用个人用户身份验证。问题是当我登录后,我仍然可以进入登录页面。如何防止它?

您可以自定义一个中间件,以便在用户登录并访问登录页面时重定向

重定向中间件.cs

Startup.cs


我实现了你的答案。问题是RedirectMiddleware类中的IsAuthenticated方法即使在经过身份验证后仍抛出false。@PrabeshUpreti确保app.useMidleware;是afterapp.UseAuthentication;
public class RedirectMiddleware
{
    private readonly RequestDelegate _next;

    public RedirectMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        if(context.Request.Path == "/Identity/Account/Login")
        {
            if (context.User.Identity.IsAuthenticated)
            {
                context.Response.Redirect("/Home/Index");
            }
        }
        await _next.Invoke(context);
    }
}
app.UseMiddleware<RedirectMiddleware>();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    endpoints.MapRazorPages();
});
public async Task OnGetAsync(string returnUrl = null)
    {
        if (!string.IsNullOrEmpty(ErrorMessage))
        {
            ModelState.AddModelError(string.Empty, ErrorMessage);
        }
        if (User.Identity.IsAuthenticated)
        {
            Response.Redirect("/Home/Index");
        }
        returnUrl = returnUrl ?? Url.Content("~/");

        // Clear the existing external cookie to ensure a clean login process
        await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

        ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

        ReturnUrl = returnUrl;
    }