ASP.NET MVC JsonResult and AuthorizeAttribute

What is the most direct way to use AuthorizeAttributeit JsonResulttogether so that when the user is not logged in, the application returns a Json error, not a login page?

The two things that I am considering now are expanding AuthorizeAttributeor simply creating a new attribute that implements IAuthorizationFilter.

+3
source share
1 answer

Remove AuthorizeAttributefrom your action.

Then in the first lines of your action, paste this:

if (!User.Identity.IsAuthenticated)
    return Json("Need to login");

or return any message you want.

+3
source

Source: https://habr.com/ru/post/1788411/


All Articles