简体   繁体   English

MVC3中基于角色的授权的管理/文档

[英]Management/Documentation of Role-Based Authorization in MVC3

I'm new to a project and have inherited a site that uses role-based authorization. 我是一个项目的新手,并且继承了一个使用基于角色的授权的网站。 There are a lot of Authorize attributes on the methods in the code - for example: 代码中的方法上有很多Authorize属性-例​​如:

public class MyController:Controller
{
    [Authorize(Roles = "Manager")]
    [Authorize(Roles = "Admin")]
    public ActionResult Action1()
    {
        //...
    }
}

I want to sort of have authorization definitions for each of these roles like: 我想为每个角色都具有授权定义,例如:

  • Manager - can edit/delete an order 管理员-可以编辑/删除订单
  • Admin - can edit/delete users 管理员-可以编辑/删除用户

I was wondering if there is any special MVC/Visual Studio/ReSharper function that can produce a list of methods that have particular roles or if compiling somehow generates documentation somewhere that I can glance at. 我想知道是否有任何特殊的MVC / Visual Studio / ReSharper函数可以生成具有特定角色的方法列表,或者是否以某种方式进行编译可以在我可以浏览的地方生成文档。 I have looked around for something to help with no luck. 我到处寻找可以帮助自己的运气。

Short Answer 简短答案

I am not aware of a Visual Studio or MVC feature or a plugin (including ReSharper) to do what you want directly; 我不知道Visual Studio或MVC功能或插件(包括ReSharper)可以直接做您想做的事情; but Visual Studio and .NET provide the basic building blocks to do it - eg regular expressions and reflection. 但是Visual Studio和.NET提供了执行此操作的基本构件-例如正则表达式和反射。

VS Find in Files (Using Regular Expressions) VS在文件中查找(使用正则表达式)

Overview 概观

The easiest (if not the most elegant) way to do what you want is to simply search your project or solution with Visual Studio's Find in Files dialog ( Edit > Find and Replace > Find in Files or Ctrl + Shift + F ) using a regular expression... 做您想要的事情的最简单(即使不是最优雅的)方法是使用常规的Visual Studio的“在文件中查找”对话框(“ 编辑”>“查找和替换”>“在文件中查找”Ctrl + Shift + F )简单地搜索项目或解决方案表达...

VS使用正则表达式查找文件

..., which will return results like the following: ...,它将返回如下结果:

Find all "(?<=\[[HttpGet]+\]\s+(\[[A-Za-z_]+\]\s+)*)(public\s+)(?!(class|interface)\s+)[A-Za-z_<>]+\s+[A-Za-z_]+\s*\(", Regular expressions, Subfolders, Find Results 1, Entire Solution, "*.cs"
  C:\Users\me\Source\Repo\Solution\Controllers\Controller.cs(21):        public List<SomeType> Action1() {
  C:\Users\me\Source\Repo\Solution\Controllers\Controller.cs(30):        public List<Foo> Action2() {
  C:\Users\me\Source\Repo\Solution\Controllers\Controller.cs(38):        public List<Bar> Action3() {
  C:\Users\me\Source\Repo\Solution\Controllers\AnotherController.cs(46):        public List<Bar> ActionA(int n) {
  C:\Users\me\Source\Repo\Solution\Controllers\AnotherController.cs(78):        public List<Baz> ActionB() {
  C:\Users\me\Source\Repo\Solution\Controllers\YetAnotherController.cs(35):        public SomeOtherType ActionAlpha() {
  C:\Users\me\Source\Repo\Solution\Controllers\YetAnotherController.cs(51):        public List<SomeType> ActionBeta(int n) {
  Matching lines: 7    Matching files: 3    Total files searched: 32

Here I searched for public methods with [HttpGet] . 在这里,我使用[HttpGet]搜索了公共方法。 (Also note that I did this in VS 2013, which uses standard .NET regular expressions for Find in Files unlike previous versions .) (还请注意,我是在VS 2013中执行此操作的, 与以前的版本不同 ,VS 2013 在文件中使用标准.NET正则表达式查找文件 。)

Applied to Your Case 适用于您的案例

For your case centered on [Authorize(Roles = "<role>"] , I would start with the following regular expression for all methods with the attribute... 对于以[Authorize(Roles = "<role>"]为中心的情况,我将从以下带有属性的所有方法的正则表达式开始...

(?<=\[Authorize\(Roles = "[A-Za-z]"\)\]\s+)(public\s+)(?!(class|interface)\s+)[A-Za-z_<>]+\s+[A-Za-z_]+\s*\(

...and a narrower variant of it for methods with the attribute and a specific Roles argument (eg "Admin" ): ...以及带有属性和特定Roles参数(例如"Admin" )的方法的较窄变体:

(?<=\[Authorize\(Roles = "Admin"\)\]\s+)(public\s+)(?!(class|interface)\s+)[A-Za-z_<>]+\s+[A-Za-z_]+\s*\(

Tweaks to the regular expressions above will probably improve your results (eg character classes like [A-Za-z_] , Roles *= *" instead of Roles = " etcetera); 调整上面的正则表达式可能会改善您的结果(例如[A-Za-z_]类的字符类, Roles *= *"而不是Roles = " etcetera); but they should provide a good start at least; 但是它们至少应该提供一个良好的开端; I checked them against AuthorizeAttribute with various Roles arguments. 我使用各种Roles参数对照AuthorizeAttribute检查了它们。

If you have trouble understanding or tweaking the above regular expressions, MSDN's Regular Expression Language - Quick Reference and other online resources should prove helpful. 如果您在理解或调整上面的正则表达式时遇到困难,则MSDN的“ 正则表达式语言-快速参考”和其他联机资源应该会有所帮助。

Other Possibilities 其他可能性

A couple other ways to approach what you want that come to mind (among many others) are: 可以想到的其他几种方法(包括许多其他方法)是:

  • Writing a quick tool that uses .NET reflection. 编写一个使用.NET反射的快速工具。
  • Using NDepend's CQL . 使用NDepend的CQL

Edit 编辑

Per my comment on your question that it is simply phenomenal, I realized that I had not stopped to do a basic search to challenge my experience. 根据我对您的问题的看法,这简直是惊人的,我意识到我并没有停止做一个基本的探索来挑战我的经历。 :/ :/

It turns out that a Jigar.Net article entitled Authorization in ASP.Net MVC using XML Configuration describes a cool, different way to manage/document authorization - proactively using a dedicated XML configuration section like the following... 事实证明,Jigar.Net的一篇题为《 使用XML配置在ASP.Net MVC中进行授权》描述了一种很酷的,不同的方式来管理/文档授权- 主动使用专用的XML配置部分,如下所示...

< controllers > 
   < controller  name ="Admin"> 
     < roles > 
       < role > Admin </ role > 
       < role > Edit </ role > 
       < role > View </ role > 
     </ roles > 
     < actions > 
       < action  name ="Index"> 
         < roles > 
           < role > Admin </ role > 
           < role > Edit </ role > 
           < role > View </ role > 
         </ roles > 
       </ action > 
       < action  name ="Edit"> 
         < roles > 
           < role > Edit </ role > 
           < role > Admin </ role > 
         </ roles > 
       </ action > 
       < action  name ="Admin"> 
         < roles > 
           < role > Admin </ role > 
         </ roles > 
       </ action > 
     </ actions > 
   </ controller > 
   < controller  name ="Home"> </ controller > 
</ controllers >

...paired with supporting code. ...与支持代码配对 (The article also underscores that a database is of course an alternative to an XML config section.) (本文还强调了,数据库当然是XML配置节的替代方案。)

I plan to consider this question further and look for other approaches & tools; 我打算进一步考虑这个问题,并寻找其他方法和工具; hopefully someone can answer with even greater insight; 希望有人能够以更大的洞察力回答; but this is what I quickly found once I tried. 但这是我尝试后很快发现的。 :} :}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM