开发者

ASP.NET MVC JsonResult and AuthorizeAttribute

开发者 https://www.devze.com 2023-02-07 09:34 出处:网络
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

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号