ASP.NET MVC JsonResult和AuthorizeAttribute [英] ASP.NET MVC JsonResult and AuthorizeAttribute
本文介绍了ASP.NET MVC JsonResult和AuthorizeAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是用最直接的方式 AuthorizeAttribute
和 JsonResult
在一起,这样当用户未授权应用程序将返回一个JSON错误,而不是一个登录页面?
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?
的两件事情,我目前正在考虑要扩展 AuthorizeAttribute
或只是让一个新的属性,实现个IAuthorizationFilter
。
The two things I am currently considering are extending AuthorizeAttribute
or just making a new attribute that implements IAuthorizationFilter
.
推荐答案
从你的行动中删除 AuthorizeAttribute
。
然后在你的行动的第一线,插入此:
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.
这篇关于ASP.NET MVC JsonResult和AuthorizeAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文