Asp MVC [授权] 返回 Post 而不是 Get
我想在我的控制器方法上使用 [Authorize(Roles="Admin")] 标签。
如果用户不是管理员,我想将该用户返回到我的登录屏幕。 将用户返回到我的登录页面的默认行为是使用 Get url 将我的用户重新路由到“帐户/登录”。
问题是,我网站的子页面都是由 Ajax 调用刷新的部分视图,包括我的登录屏幕。
所以我的问题是: 是否可以更改下面的类以返回 post 重定向而不是 get 重定向?
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");
}
}
}
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");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 Microsoft.WebPages.PreApplicationStartCode.SetupFormsAuthentication() 中找到了一种解决方案,
只需添加一个名为“loginUrl”的 appSetting 即可指定登录操作:
I found a solution in
Microsoft.WebPages.PreApplicationStartCode.SetupFormsAuthentication()
One need only add an appSetting named "loginUrl" to specify the login action:
显然,问题似乎通过删除
我的帐户控制器的登录方法上的属性得到解决。
这样我们甚至不必重写 AuthorizeAttribute
:)
Apparently the problem seemes solved by removing the
attribute on my Account controller's Login method.
This way we don't even have to override the AuthorizeAttribute
:)