Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I’ve tried to improve my site’s security, & one of things I’ve tried is when a user is not admin & wants to access admin page, system returns NotFound. This makes impossible to a hacker to know your admin page. But how to do it?
This is what I’ve tried to. I’ve made a Middleware in program.cs to check for URL & redirect somewhere, which isn’t what I want. Even I’ve tried to set the status code to 404, but that doesn’t works. what I want to access here, is return NotFound (); Method. Is there a way to do it. Thanks

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 ();
});

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

// Write response
context.Response.StatusCode = ((int)HttpStatusCode.NotFound);

return;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading