授权属性VS web.config中的授权节点 [英] Authorize attribute vs authorization node in web.config

查看:136
本文介绍了授权属性VS web.config中的授权节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用web.config中的授权标签限制访问一个ASP.NET MVC 3应用程序


   <身份验证模式=窗口>< /认证>
    < roleManager启用=真正的defaultProvider =AspNetWindowsTokenRoleProvider/>
    <授权>
      <让角色=MYDOMAIN \\ MyGroup的/>
      <拒绝用户=*/>
      <拒绝用户=? />
    < /授权>

或装饰用[授权()]属性控制器基类(甚至用自定义授权属性)

  [AdminOnly]
公共类BaseController:控制器{}

问题是:它们是替代和等效的方法?如果我总是用一种方法,而不是其他?我应该保持它的元素一点?


解决方案

  

我知道我可以使用web.config中的授权标签限制访问一个ASP.NET MVC 3应用程序


没有,不要在ASP.NET MVC使用。


  

现在的问题是:他们的替代和等效方法


没有,他们没有选择。你不应该使用<授权>在在ASP.NET MVC应用程序的web.config 标签,因为它是基于路径,而MVC可以与控制器动作和路线。做授权的ASP.NET MVC的正确方法是使用 [授权] 属性。

I know I can restrict the access to an ASP.NET MVC 3 application using the authorization tag in web.config

   <authentication mode="Windows"></authentication>
    <roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider" />
    <authorization>
      <allow roles="MyDomain\MyGroup" />
      <deny users="*" />
      <deny users="?" />
    </authorization>

or decorating the controller base class with an [Authorize()] attribute (or even with a custom Authorize attribute)

[AdminOnly]
public class BaseController : Controller{}

The question is: are they alternative and equivalent approaches? Should I always use one approach rather than the other? Which elements should I keep in mind?

解决方案

I know I can restrict the access to an ASP.NET MVC 3 application using the authorization tag in web.config

No, don't use this in ASP.NET MVC.

The question is: are they alternative and equivalent approaches?

No, they are not alternative. You should not use the <authorization> tag in web.config in an ASP.NET MVC application because it is based on paths, whereas MVC works with controller actions and routes. The correct way to do authorization in ASP.NET MVC is using the [Authorize] attribute.

这篇关于授权属性VS web.config中的授权节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆