繁体   English   中英

如何返回在 Program.cs ASP.Net core 6 中找不到

[英]How to return not found in Program.cs ASP.Net core 6

我试图提高我网站的安全性,我尝试过的一件事是当用户不是管理员并且想要访问管理页面时,系统返回NotFound 这使得黑客不可能知道您的管理页面。 但是怎么做呢? 这是我尝试过的。 我在 program.cs 中制作了一个中间件来检查 URL 并在某处重定向,这不是我想要的。 即使我尝试将状态码设置为 404,但这不起作用。 我想在这里访问的是return NotFound (); 方法。 有没有办法做到这一点。 谢谢

app.Use (async (context, next) =>
{
    if (context.Request.Path.StartsWithSegments ("/Admin"))
    {
        if (/* Checking if user is not admin */)
        {
            // context.Response.Redirect ("/");
            // The code to do same as return NotFound ();
        }
    }
    await next.Invoke ();
});
// Write response
context.Response.StatusCode = ((int)HttpStatusCode.NotFound);

return;

还有一种更好的方法我意识到不需要使用 Microsoft.Net:

context.Response.StatusCode = 404;
return;
context.Context.Response.StatusCode = StatusCodes.Status404NotFound;

return;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM