ASP MVC [授权]返回一个帖子,而不是获取的 [英] Asp MVC [Authorize] to return a Post instead of a Get
问题描述
我想用我的控制器方法[授权(角色=管理员)的标签。
如果用户不是管理员,我想该用户回到我的登录屏幕。
用户回到我的登录页面的默认行为是重新路由我的用户账号/登录使用取得网址。
问题是,我的网站的子页面都是局部视图通过Ajax调用,包括我的登录屏幕刷新。
所以我的问题是:
是否有可能改变下面的类返回后重定向,而不是让重定向?
公共类AjaxAuthorizeAttribute:AuthorizeAttribute
{
覆盖公共无效OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
//只有做一些事情,如果我们要给予HttpUnauthorizedResult,我们在AJAX模式。
如果(filterContext.Result是HttpUnauthorizedResult&放大器;&放大器; filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.Result =新RedirectResult(../帐户/登录);
}
}
}
显然,问题seemes通过删除解决了
的[AcceptVerbs(HttpVerbs.Post)
在我的帐户控制器的登录方法的属性。
这个方式,我们甚至不用覆盖AuthorizeAttribute
:)
I would like to use [Authorize(Roles="Admin")] tags on my controller methods.
If a user is not an admin I would like to return this user to my login screen. The default behaviour of returning the user to my login page is reroute my user to "Account/Login" using a Get url.
The problem is, my website's subpages are all partial views refreshed by Ajax calls, including my login screen.
So my question is: Is it possible to alter the class below to return a post redirect instead of a get redirect?
public class AjaxAuthorizeAttribute : AuthorizeAttribute
{
override public void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
// Only do something if we are about to give a HttpUnauthorizedResult and we are in AJAX mode.
if (filterContext.Result is HttpUnauthorizedResult && filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.Result = new RedirectResult("../Account/Login");
}
}
}
Apparently the problem seemes solved by removing the
[Acceptverbs(HttpVerbs.Post)]
attribute on my Account controller's Login method.
This way we don't even have to override the AuthorizeAttribute
:)
这篇关于ASP MVC [授权]返回一个帖子,而不是获取的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!