ASP.NET MVC JsonResult and AuthorizeAttribute

心已入冬 提交于 2019-12-10 20:57:03

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/4825550/asp-net-mvc-jsonresult-and-authorizeattribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!