关于管理员[授权]属性的问题

发布于 2024-11-03 04:55:49 字数 370 浏览 11 评论 0原文

如果我的 ASP.NET MVC 3 (Razor) Web 应用程序中有一个 区域,其中所有控制器都派生自如下所示的基本控制器:

[Authorize(Roles="Administrator")]
public class AdminController : Controller
{

}

当非管理员尝试访问该区域中的 URL 时区域,他们会被重定向到 web.config 中指定的登录页面。

但是,如果用户已经通过身份验证,但不是管理员,则这实际上没有意义。在这种情况下,我们不应该返回 HTTP 401 吗?

我的问题基本上是人们如何处理这个问题 - 他们是否创建自定义授权属性?

If i have an Area in my ASP.NET MVC 3 (Razor) Web application, where all controllers derive from a base controller that looks like this:

[Authorize(Roles="Administrator")]
public class AdminController : Controller
{

}

When a non-administrator tries to access a URL in that area, they get redirected to the login page specified in the web.config.

But this doesn't really make sense if the user is already authenticated, but not an administrator. In that scenario, shouldn't we be returned a HTTP 401?

My question is basically how do people handle this - do they create custom authorize attributes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

守护在此方 2024-11-10 04:55:49

看到这个线程...
ASP.Net 将 401 错误代码转换为 302 错误代码

您真正想要做的是返回 403 代码。 401 用于身份验证质询。 ASP.NET 表单授权拦截 401 并将用户推送到登录页面。

如果您仍然想做 401,您能否描述一下最终用户的预期体验是什么?

See this thread ...
ASP.Net converts 401 to 302 error codes

What you really want to do is return a 403 code. 401 is intended for authentication challenges. ASP.NET forms authorization intercepts 401 and pushes users to the login page.

If you still want to do a 401, could you describe what is the expected experience for the end user?

离去的眼神 2024-11-10 04:55:49

我使用 ASP.NET MVC4 Beta,今天我注意到,如果我将 ReturnUrl 参数添加到查询字符串,表单模块不会更改响应。

因此,如果操作 i/Rate 具有属性 [Authorize],则

<a href="/xm/i/Rate?pid=3&count=2&ReturnUrl=%2F">..</a>

返回 401。我不知道它是错误还是功能,但现在它按描述工作。

I work with ASP.NET MVC4 Beta and today I noticed that if I add ReturnUrl parameter to querystring, the forms module doesn't change the response.

So if action i/Rate has attribute [Authorize] then

<a href="/xm/i/Rate?pid=3&count=2&ReturnUrl=%2F">..</a>

returns 401. I don't know if it is bug, or feature, but now it works as described.

调妓 2024-11-10 04:55:49

我们有针对此类场景的自定义授权过滤器属性,并将用户带到自定义错误页面

public void OnAuthorization(AuthorizationContext filterContext) {


if(//user does not have permission){

filterContext.Result = new RedirectResult("/Error/AccessDenied");

}

we have custom authorize filter attribute for such scenarios and we take user to custom error page

public void OnAuthorization(AuthorizationContext filterContext) {


if(//user does not have permission){

filterContext.Result = new RedirectResult("/Error/AccessDenied");

}
白色秋天 2024-11-10 04:55:49

如果未经授权的用户尝试访问标记有 Authorize 属性的方法,MVC 框架将返回 401 HTTP 状态代码。如果站点配置为使用 ASP.NET 表单身份验证,则 401 状态代码会导致浏览器将用户重定向到登录页面。

参考:http://msdn.microsoft.com/ zh-tw/library/system.web.mvc.authorizeattribute.aspx

其他类似问题:如何在 ASP.NET MVC 中拦截表单身份验证的 401?

If an unauthorized user tries to access a method that is marked with the Authorize attribute, the MVC framework returns a 401 HTTP status code. If the site is configured to use ASP.NET forms authentication, the 401 status code causes the browser to redirect the user to the login page.

Refrence:http://msdn.microsoft.com/zh-tw/library/system.web.mvc.authorizeattribute.aspx

Other similar question: How to intercept 401 from Forms Authentication in ASP.NET MVC?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文