fetus-elitus
fetus-elitus

Reputation: 41

How can I implement a redirection for an anonymous user to a different view in ASP.NET Core 6 MVC if they don't have a required cookie?

I am trying to restrict access to a specific URL (/xyz) for anonymous users who do not possess a specific GUID cookie. To handle this, I created a middleware that assigns the cookie via HttpContext for users who do not have it and are not authenticated using Identity Framework.

To implement this functionality, I wanted to utilize authorization filters and policies, but I am unsure how to redirect anonymous users without the required cookie to a custom view, different than the one used by Identity Framework default schema.

I have come across information suggesting that I can use IAuthorizationMiddlewareResultHandler to solve my issue, but I am uncertain if this is the most appropriate solution. Additionally, I have seen others creating default cookie schemas for authentication, but I am unsure if these can be utilized for anonymous users, or how to assign them within a custom middleware.

Upvotes: 0

Views: 109

Answers (1)

buga
buga

Reputation: 1344

in the XyzController/Index action

if (noCookiePresent) return RedirectToAction("Index", "Home");

Upvotes: 1

Related Questions