knpwrs
knpwrs

Reputation: 16456

ASP.NET MVC JsonResult and AuthorizeAttribute

What is the most straight forward way to use AuthorizeAttribute and JsonResult together so that when the user is not authorized the application returns a Json error rather than a log in page?

The two things I am currently considering are extending AuthorizeAttribute or just making a new attribute that implements IAuthorizationFilter.

Upvotes: 3

Views: 2650

Answers (1)

ajma
ajma

Reputation: 12206

Remove the AuthorizeAttribute from your Action.

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

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

or return whatever message you want.

Upvotes: 3

Related Questions