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



我试图提高我的网站的安全性,&我尝试过的一件事是当用户不是admin&想要访问管理页面,系统返回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 ();
});
context.Response.StatusCode = StatusCodes.Status404NotFound;
return;
// Write response
context.Response.StatusCode = ((int)HttpStatusCode.NotFound);
return;

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

context.Response.StatusCode = 404;
return;

最新更新