[转载]Asp.Net MVC 权限控制(三):Controller和Action级别控制 - Jetlian - 博客园

baacloud免费翻墙vpn注册使用

来源: [转载]Asp.Net MVC 权限控制(三):Controller和Action级别控制 – Jetlian – 博客园

再次在重构!这次对Controller和Action进行验证。

思路:系统有很多功能集,功能集对应很多Controller和Action角色分配很多功能集


首先构建一个基础数据:

1.功能集初始化:

<div class="container">
<div class="line number1 index0 alt2"><code class="csharp color1">/// &lt;summary&gt;</code></div>
<div class="line number2 index1 alt1"><code class="csharp spaces">  </code><code class="csharp color1">/// 系统模块</code></div>
<div class="line number3 index2 alt2"><code class="csharp spaces">  </code><code class="csharp color1">/// &lt;/summary&gt;</code></div>
<div class="line number4 index3 alt1"><code class="csharp spaces">  </code><code class="csharp keyword">public</code> <code class="csharp keyword">class</code> <code class="csharp plain">SystemModule</code></div>
<div class="line number5 index4 alt2"><code class="csharp spaces">  </code><code class="csharp plain">{</code></div>
<div class="line number6 index5 alt1"><code class="csharp spaces">      </code><code class="csharp keyword">public</code> <code class="csharp plain">SystemModule()</code></div>
<div class="line number7 index6 alt2"><code class="csharp spaces">      </code><code class="csharp plain">{</code></div>
<div class="line number8 index7 alt1"><code class="csharp spaces">          </code><code class="csharp keyword">this</code><code class="csharp plain">.ID = Guid.NewGuid();</code></div>
<div class="line number9 index8 alt2"><code class="csharp spaces">      </code><code class="csharp plain">}</code></div>
<div class="line number10 index9 alt1"></div>
<div class="line number11 index10 alt2"><code class="csharp spaces">      </code><code class="csharp keyword">public</code> <code class="csharp plain">Guid ID { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number12 index11 alt1"></div>
<div class="line number13 index12 alt2"><code class="csharp spaces">      </code><code class="csharp keyword">public</code> <code class="csharp keyword">string</code> <code class="csharp plain">Name { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number14 index13 alt1"></div>
<div class="line number15 index14 alt2"><code class="csharp spaces">      </code><code class="csharp keyword">public</code> <code class="csharp keyword">string</code> <code class="csharp plain">Description { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number16 index15 alt1"></div>
<div class="line number17 index16 alt2"><code class="csharp spaces">      </code><code class="csharp keyword">public</code> <code class="csharp plain">SystemModule Parent { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number18 index17 alt1"></div>
<div class="line number19 index18 alt2"><code class="csharp spaces">      </code><code class="csharp keyword">public</code> <code class="csharp plain">List&lt;SystemModuleController&gt; SystemModuleControllers { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number20 index19 alt1"></div>
<div class="line number21 index20 alt2"><code class="csharp spaces">      </code><code class="csharp keyword">public</code> <code class="csharp keyword">static</code> <code class="csharp plain">List&lt;SystemModule&gt; Init()</code></div>
<div class="line number22 index21 alt1"><code class="csharp spaces">      </code><code class="csharp plain">{</code></div>
<div class="line number23 index22 alt2"><code class="csharp spaces">          </code><code class="csharp keyword">var</code> <code class="csharp plain">m1 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModule { Name = </code><code class="csharp string">"资源监测"</code> <code class="csharp plain">};</code></div>
<div class="line number24 index23 alt1"><code class="csharp spaces">          </code><code class="csharp keyword">var</code> <code class="csharp plain">m2 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModule { Name = </code><code class="csharp string">"规划管理"</code> <code class="csharp plain">};</code></div>
<div class="line number25 index24 alt2"></div>
<div class="line number26 index25 alt1"><code class="csharp spaces">          </code><code class="csharp keyword">var</code> <code class="csharp plain">c1 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModuleController { ControllerName = </code><code class="csharp string">"PlanManagement"</code><code class="csharp plain">, ActionName = </code><code class="csharp string">"Search"</code> <code class="csharp plain">};</code></div>
<div class="line number27 index26 alt2"><code class="csharp spaces">          </code><code class="csharp keyword">var</code> <code class="csharp plain">c2 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModuleController { ControllerName = </code><code class="csharp string">"PlanManagement"</code><code class="csharp plain">, ActionName = </code><code class="csharp string">"Add"</code> <code class="csharp plain">};</code></div>
<div class="line number28 index27 alt1"><code class="csharp spaces">          </code><code class="csharp keyword">var</code> <code class="csharp plain">c3 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModuleController { ControllerName = </code><code class="csharp string">"PlanManagement"</code><code class="csharp plain">, ActionName = </code><code class="csharp string">"Edit"</code> <code class="csharp plain">};</code></div>
<div class="line number29 index28 alt2"><code class="csharp spaces">          </code><code class="csharp keyword">var</code> <code class="csharp plain">c4 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModuleController { ControllerName = </code><code class="csharp string">"PlanManagement"</code><code class="csharp plain">, ActionName = </code><code class="csharp string">"Delete"</code> <code class="csharp plain">};</code></div>
<div class="line number30 index29 alt1"><code class="csharp spaces">          </code><code class="csharp keyword">var</code> <code class="csharp plain">c5 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModuleController { ControllerName = </code><code class="csharp string">"PlanManagement"</code><code class="csharp plain">, ActionName = </code><code class="csharp string">"Approval"</code> <code class="csharp plain">};</code></div>
<div class="line number31 index30 alt2"></div>
<div class="line number32 index31 alt1"><code class="csharp spaces">          </code><code class="csharp keyword">var</code> <code class="csharp plain">m21 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModule { Name = </code><code class="csharp string">"规划信息查询"</code><code class="csharp plain">, Parent = m2, SystemModuleControllers = </code><code class="csharp keyword">new</code> <code class="csharp plain">List&lt;SystemModuleController&gt; { c1 } };</code></div>
<div class="line number33 index32 alt2"><code class="csharp spaces">          </code><code class="csharp keyword">var</code> <code class="csharp plain">m22 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModule { Name = </code><code class="csharp string">"规划信息管理"</code><code class="csharp plain">, Parent = m2, SystemModuleControllers = </code><code class="csharp keyword">new</code> <code class="csharp plain">List&lt;SystemModuleController&gt; { c2, c3, c4 } };</code></div>
<div class="line number34 index33 alt1"><code class="csharp spaces">          </code><code class="csharp keyword">var</code> <code class="csharp plain">m23 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModule { Name = </code><code class="csharp string">"规划辅助审批"</code><code class="csharp plain">, Parent = m2, SystemModuleControllers = </code><code class="csharp keyword">new</code> <code class="csharp plain">List&lt;SystemModuleController&gt; { c5 } };</code></div>
<div class="line number35 index34 alt2"></div>
<div class="line number36 index35 alt1"><code class="csharp spaces">          </code><code class="csharp keyword">return</code> <code class="csharp keyword">new</code> <code class="csharp plain">List&lt;SystemModule&gt; { m1, m2, m12, m21, m22, m23 };</code></div>
<div class="line number37 index36 alt2"><code class="csharp spaces">      </code><code class="csharp plain">}</code></div>
<div class="line number38 index37 alt1"><code class="csharp spaces">  </code><code class="csharp plain">}</code></div>
<div class="line number38 index37 alt1">
2.角色初始化:
</div>
<div class="line number38 index37 alt1">
<div class="container">
<div class="line number1 index0 alt2"><code class="csharp color1">/// &lt;summary&gt;</code></div>
<div class="line number2 index1 alt1"><code class="csharp color1">/// 角色</code></div>
<div class="line number3 index2 alt2"><code class="csharp color1">/// &lt;/summary&gt;</code></div>
<div class="line number4 index3 alt1"><code class="csharp keyword">public</code> <code class="csharp keyword">class</code> <code class="csharp plain">SystemRole</code></div>
<div class="line number5 index4 alt2"><code class="csharp plain">{</code></div>
<div class="line number6 index5 alt1"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp plain">SystemRole()</code></div>
<div class="line number7 index6 alt2"><code class="csharp spaces">    </code><code class="csharp plain">{</code></div>
<div class="line number8 index7 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">this</code><code class="csharp plain">.ID = Guid.NewGuid();</code></div>
<div class="line number9 index8 alt2"><code class="csharp spaces">    </code><code class="csharp plain">}</code></div>
<div class="line number10 index9 alt1"></div>
<div class="line number11 index10 alt2"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp plain">Guid ID { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number12 index11 alt1"></div>
<div class="line number13 index12 alt2"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp keyword">string</code> <code class="csharp plain">Name { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number14 index13 alt1"></div>
<div class="line number15 index14 alt2"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp keyword">string</code> <code class="csharp plain">Description { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number16 index15 alt1"></div>
<div class="line number17 index16 alt2"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp plain">List&lt;SystemModule&gt; SystemModules { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number18 index17 alt1"></div>
<div class="line number19 index18 alt2"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp keyword">static</code> <code class="csharp plain">SystemRole Init(</code><code class="csharp keyword">string</code><code class="csharp plain">[] roles)</code></div>
<div class="line number20 index19 alt1"><code class="csharp spaces">    </code><code class="csharp plain">{</code></div>
<div class="line number21 index20 alt2"><code class="csharp spaces">        </code><code class="csharp keyword">var</code> <code class="csharp plain">modules = SystemModule.Init();</code></div>
<div class="line number22 index21 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">var</code> <code class="csharp plain">systemModules = roles.Select(r =&gt; modules.FirstOrDefault(m =&gt; m.Name == r)).ToList();</code></div>
<div class="line number23 index22 alt2"><code class="csharp spaces">        </code><code class="csharp keyword">var</code> <code class="csharp plain">role = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemRole { Name = </code><code class="csharp string">"默认角色"</code><code class="csharp plain">, SystemModules = systemModules };</code></div>
<div class="line number24 index23 alt1"></div>
<div class="line number25 index24 alt2"><code class="csharp spaces">        </code><code class="csharp keyword">return</code> <code class="csharp plain">role;</code></div>
<div class="line number26 index25 alt1"><code class="csharp spaces">    </code><code class="csharp plain">}</code></div>
<div class="line number27 index26 alt2"><code class="csharp plain">}</code></div>
<div class="line number27 index26 alt2">
3. 系统所有Controller和Action的读取
</div>
<div class="line number27 index26 alt2">
<div class="container">
<div class="line number1 index0 alt2"><code class="csharp color1">/// &lt;summary&gt;</code></div>
<div class="line number2 index1 alt1"><code class="csharp spaces">   </code><code class="csharp color1">/// 读取系统的所有Controller和Action</code></div>
<div class="line number3 index2 alt2"><code class="csharp spaces">   </code><code class="csharp color1">/// &lt;/summary&gt;</code></div>
<div class="line number4 index3 alt1"><code class="csharp spaces">   </code><code class="csharp keyword">public</code><code class="csharp keyword">class</code><code class="csharp plain">SystemModuleController</code></div>
<div class="line number5 index4 alt2"><code class="csharp spaces">   </code><code class="csharp plain">{</code></div>
<div class="line number6 index5 alt1"><code class="csharp spaces">       </code><code class="csharp keyword">public</code><code class="csharp plain">SystemModuleController()</code></div>
<div class="line number7 index6 alt2"><code class="csharp spaces">       </code><code class="csharp plain">{</code></div>
<div class="line number8 index7 alt1"><code class="csharp spaces">           </code><code class="csharp keyword">this</code><code class="csharp plain">.ID = Guid.NewGuid();</code></div>
<div class="line number9 index8 alt2"><code class="csharp spaces">       </code><code class="csharp plain">}</code></div>
<div class="line number11 index10 alt2"><code class="csharp spaces">       </code><code class="csharp keyword">public</code><code class="csharp plain">Guid ID { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number13 index12 alt2"><code class="csharp spaces">       </code><code class="csharp keyword">public</code><code class="csharp keyword">string</code><code class="csharp plain">ModuleName { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number15 index14 alt2"><code class="csharp spaces">       </code><code class="csharp keyword">public</code><code class="csharp keyword">string</code><code class="csharp plain">ControllerName { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number17 index16 alt2"><code class="csharp spaces">       </code><code class="csharp keyword">public</code><code class="csharp keyword">string</code><code class="csharp plain">ActionName { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number19 index18 alt2"><code class="csharp spaces">       </code><code class="csharp keyword">public</code><code class="csharp keyword">string</code><code class="csharp plain">Description { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number21 index20 alt2"><code class="csharp spaces">       </code><code class="csharp keyword">public</code><code class="csharp plain">List&lt;SystemModuleController&gt; SystemModuleActions { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number23 index22 alt2"><code class="csharp spaces">       </code><code class="csharp keyword">public</code><code class="csharp keyword">static</code><code class="csharp plain">List&lt;SystemModuleController&gt; GetSystemModuleController()</code></div>
<div class="line number24 index23 alt1"><code class="csharp spaces">       </code><code class="csharp plain">{</code></div>
<div class="line number25 index24 alt2"><code class="csharp spaces">           </code><code class="csharp keyword">var</code><code class="csharp plain">systemModuleControllers = </code><code class="csharp keyword">new</code><code class="csharp plain">List&lt;SystemModuleController&gt;();</code></div>
<div class="line number26 index25 alt1"><code class="csharp spaces">           </code><code class="csharp comments">// 读取项目中的Controller</code></div>
<div class="line number27 index26 alt2"><code class="csharp spaces">           </code><code class="csharp keyword">var</code><code class="csharp plain">types = Assembly.Load(</code><code class="csharp string">"PRMMS.Authorization"</code><code class="csharp plain">).GetTypes().Where(b =&gt; b.BaseType != </code><code class="csharp keyword">null</code><code class="csharp plain">&amp;&amp; b.BaseType.Name == </code><code class="csharp string">"BaseController"</code><code class="csharp plain">);</code></div>
<div class="line number28 index27 alt1"><code class="csharp spaces">           </code><code class="csharp keyword">foreach</code><code class="csharp plain">(</code><code class="csharp keyword">var</code><code class="csharp plain">type </code><code class="csharp keyword">in</code><code class="csharp plain">types)</code></div>
<div class="line number29 index28 alt2"><code class="csharp spaces">           </code><code class="csharp plain">{</code></div>
<div class="line number30 index29 alt1"><code class="csharp spaces">               </code><code class="csharp comments">// 标记需要权限验证的Controller</code></div>
<div class="line number31 index30 alt2"><code class="csharp spaces">               </code><code class="csharp keyword">var</code><code class="csharp plain">modules = type.GetCustomAttributes(</code><code class="csharp keyword">typeof</code><code class="csharp plain">(ModuleAuthorizationAttribute), </code><code class="csharp keyword">true</code><code class="csharp plain">);</code></div>
<div class="line number32 index31 alt1"><code class="csharp spaces">               </code><code class="csharp keyword">if</code><code class="csharp plain">(modules.Length == 1)</code></div>
<div class="line number33 index32 alt2"><code class="csharp spaces">               </code><code class="csharp plain">{</code></div>
<div class="line number34 index33 alt1"><code class="csharp spaces">                   </code><code class="csharp comments">// Controller名称</code></div>
<div class="line number35 index34 alt2"><code class="csharp spaces">                   </code><code class="csharp keyword">var</code><code class="csharp plain">controllerName = type.Name.Replace(</code><code class="csharp string">"Controller"</code><code class="csharp plain">, </code><code class="csharp string">""</code><code class="csharp plain">);</code></div>
<div class="line number36 index35 alt1"><code class="csharp spaces">                   </code><code class="csharp comments">// Controller描述</code></div>
<div class="line number37 index36 alt2"><code class="csharp spaces">                   </code><code class="csharp keyword">var</code><code class="csharp plain">description = </code><code class="csharp keyword">string</code><code class="csharp plain">.Empty;</code></div>
<div class="line number38 index37 alt1"><code class="csharp spaces">                   </code><code class="csharp keyword">var</code><code class="csharp plain">attrs = type.GetCustomAttributes(</code><code class="csharp keyword">typeof</code><code class="csharp plain">(System.ComponentModel.DescriptionAttribute), </code><code class="csharp keyword">true</code><code class="csharp plain">);</code></div>
<div class="line number39 index38 alt2"><code class="csharp spaces">                   </code><code class="csharp keyword">if</code><code class="csharp plain">(attrs.Length &gt; 0)</code></div>
<div class="line number40 index39 alt1"><code class="csharp spaces">                   </code><code class="csharp plain">{</code></div>
<div class="line number41 index40 alt2"><code class="csharp spaces">                       </code><code class="csharp plain">description = (attrs[0] </code><code class="csharp keyword">as</code><code class="csharp plain">System.ComponentModel.DescriptionAttribute).Description;</code></div>
<div class="line number42 index41 alt1"><code class="csharp spaces">                   </code><code class="csharp plain">}</code></div>
<div class="line number44 index43 alt1"><code class="csharp spaces">                   </code><code class="csharp comments">// 获取Controller下的Action</code></div>
<div class="line number45 index44 alt2"><code class="csharp spaces">                   </code><code class="csharp keyword">var</code><code class="csharp plain">systemModuleControllerAction = </code><code class="csharp keyword">new</code><code class="csharp plain">List&lt;SystemModuleController&gt;();</code></div>
<div class="line number46 index45 alt1"><code class="csharp spaces">                   </code><code class="csharp keyword">var</code><code class="csharp plain">actions = type.GetMethods().Where(a =&gt; a.ReturnType != </code><code class="csharp keyword">null</code><code class="csharp plain">&amp;&amp; a.ReturnType.Name == </code><code class="csharp string">"ActionResult"</code><code class="csharp plain">);</code></div>
<div class="line number47 index46 alt2"><code class="csharp spaces">                   </code><code class="csharp keyword">foreach</code><code class="csharp plain">(</code><code class="csharp keyword">var</code><code class="csharp plain">action </code><code class="csharp keyword">in</code><code class="csharp plain">actions)</code></div>
<div class="line number48 index47 alt1"><code class="csharp spaces">                   </code><code class="csharp plain">{</code></div>
<div class="line number49 index48 alt2"><code class="csharp spaces">                       </code><code class="csharp comments">// Action名称</code></div>
<div class="line number50 index49 alt1"><code class="csharp spaces">                       </code><code class="csharp keyword">var</code><code class="csharp plain">actionName = action.Name;</code></div>
<div class="line number51 index50 alt2"><code class="csharp spaces">                       </code><code class="csharp comments">// Action描述</code></div>
<div class="line number52 index51 alt1"><code class="csharp spaces">                       </code><code class="csharp keyword">var</code><code class="csharp plain">desc = </code><code class="csharp keyword">string</code><code class="csharp plain">.Empty;</code></div>
<div class="line number53 index52 alt2"><code class="csharp spaces">                       </code><code class="csharp keyword">var</code><code class="csharp plain">act = action.GetCustomAttributes(</code><code class="csharp keyword">typeof</code><code class="csharp plain">(System.ComponentModel.DescriptionAttribute), </code><code class="csharp keyword">true</code><code class="csharp plain">);</code></div>
<div class="line number54 index53 alt1"><code class="csharp spaces">                       </code><code class="csharp keyword">if</code><code class="csharp plain">(act.Length &gt; 0)</code></div>
<div class="line number55 index54 alt2"><code class="csharp spaces">                       </code><code class="csharp plain">{</code></div>
<div class="line number56 index55 alt1"><code class="csharp spaces">                           </code><code class="csharp plain">desc = (act[0] </code><code class="csharp keyword">as</code><code class="csharp plain">System.ComponentModel.DescriptionAttribute).Description;</code></div>
<div class="line number57 index56 alt2"><code class="csharp spaces">                       </code><code class="csharp plain">}</code></div>
<div class="line number58 index57 alt1"><code class="csharp spaces">                       </code><code class="csharp plain">systemModuleControllerAction.Add(</code><code class="csharp keyword">new</code><code class="csharp plain">SystemModuleController</code></div>
<div class="line number59 index58 alt2"><code class="csharp spaces">                                                            </code><code class="csharp plain">{</code></div>
<div class="line number60 index59 alt1"><code class="csharp spaces">                                                                </code><code class="csharp plain">ControllerName = controllerName,</code></div>
<div class="line number61 index60 alt2"><code class="csharp spaces">                                                                </code><code class="csharp plain">ActionName = actionName,</code></div>
<div class="line number62 index61 alt1"><code class="csharp spaces">                                                                </code><code class="csharp plain">Description = desc</code></div>
<div class="line number63 index62 alt2"><code class="csharp spaces">                                                            </code><code class="csharp plain">});</code></div>
<div class="line number64 index63 alt1"><code class="csharp spaces">                   </code><code class="csharp plain">}</code></div>
<div class="line number66 index65 alt1"><code class="csharp spaces">                   </code><code class="csharp keyword">var</code><code class="csharp plain">systemModule = </code><code class="csharp keyword">new</code><code class="csharp plain">SystemModuleController</code></div>
<div class="line number67 index66 alt2"><code class="csharp spaces">                   </code><code class="csharp plain">{</code></div>
<div class="line number68 index67 alt1"><code class="csharp spaces">                       </code><code class="csharp plain">ControllerName = controllerName,</code></div>
<div class="line number69 index68 alt2"><code class="csharp spaces">                       </code><code class="csharp plain">Description = description,</code></div>
<div class="line number70 index69 alt1"><code class="csharp spaces">                       </code><code class="csharp plain">SystemModuleActions = systemModuleControllerAction</code></div>
<div class="line number71 index70 alt2"><code class="csharp spaces">                   </code><code class="csharp plain">};</code></div>
<div class="line number72 index71 alt1"><code class="csharp spaces">                   </code><code class="csharp plain">systemModuleControllers.Add(systemModule);</code></div>
<div class="line number73 index72 alt2"><code class="csharp spaces">               </code><code class="csharp plain">}</code></div>
<div class="line number74 index73 alt1"><code class="csharp spaces">           </code><code class="csharp plain">}</code></div>
<div class="line number75 index74 alt2"><code class="csharp spaces">           </code><code class="csharp keyword">return</code><code class="csharp plain">systemModuleControllers;</code></div>
<div class="line number76 index75 alt1"><code class="csharp spaces">       </code><code class="csharp plain">}</code></div>
<div class="line number77 index76 alt2"><code class="csharp spaces">   </code><code class="csharp plain">}</code></div>
<div class="line number77 index76 alt2">
系统登录后,初始化权限并保存缓存中。
</div>
<div class="line number77 index76 alt2">
<div class="container">
<div class="line number1 index0 alt2"><code class="csharp plain">[HttpPost]</code></div>
<div class="line number2 index1 alt1"><code class="csharp plain">[ValidateAntiForgeryToken]</code></div>
<div class="line number3 index2 alt2"><code class="csharp keyword">public</code> <code class="csharp plain">ActionResult Login(LoginModel model, </code><code class="csharp keyword">string</code> <code class="csharp plain">returnUrl)</code></div>
<div class="line number4 index3 alt1"><code class="csharp plain">{</code></div>
<div class="line number5 index4 alt2"><code class="csharp spaces">    </code><code class="csharp keyword">var</code> <code class="csharp plain">userName = model.UserName;</code></div>
<div class="line number6 index5 alt1"></div>
<div class="line number7 index6 alt2"><code class="csharp spaces">    </code><code class="csharp plain">FormsAuthenticationTicket authTicket = </code><code class="csharp keyword">new</code> <code class="csharp plain">FormsAuthenticationTicket(</code></div>
<div class="line number8 index7 alt1"><code class="csharp spaces">    </code><code class="csharp plain">1,</code></div>
<div class="line number9 index8 alt2"><code class="csharp spaces">    </code><code class="csharp plain">userName,</code></div>
<div class="line number10 index9 alt1"><code class="csharp spaces">    </code><code class="csharp plain">DateTime.Now,</code></div>
<div class="line number11 index10 alt2"><code class="csharp spaces">    </code><code class="csharp plain">DateTime.Now.AddMinutes(20),</code></div>
<div class="line number12 index11 alt1"><code class="csharp spaces">    </code><code class="csharp keyword">false</code><code class="csharp plain">,</code></div>
<div class="line number13 index12 alt2"><code class="csharp spaces">    </code><code class="csharp plain">model.Roles.Aggregate((i, j) =&gt; i + </code><code class="csharp string">","</code> <code class="csharp plain">+ j)</code></div>
<div class="line number14 index13 alt1"><code class="csharp spaces">    </code><code class="csharp plain">);</code></div>
<div class="line number15 index14 alt2"><code class="csharp spaces">    </code><code class="csharp keyword">string</code> <code class="csharp plain">encryptedTicket = FormsAuthentication.Encrypt(authTicket);</code></div>
<div class="line number16 index15 alt1"><code class="csharp spaces">    </code><code class="csharp keyword">var</code> <code class="csharp plain">authCookie = </code><code class="csharp keyword">new</code> <code class="csharp plain">HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);</code></div>
<div class="line number17 index16 alt2"><code class="csharp spaces">    </code><code class="csharp plain">System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);</code></div>
<div class="line number18 index17 alt1"></div>
<div class="line number19 index18 alt2"><code class="csharp spaces">    </code><code class="csharp comments">// 初始化权限</code></div>
<div class="line number20 index19 alt1"><code class="csharp spaces">    </code><code class="csharp keyword">var</code> <code class="csharp plain">systemRole = SystemRole.Init(model.Roles);</code></div>
<div class="line number21 index20 alt2"><code class="csharp spaces">    </code><code class="csharp comments">// 缓存权限</code></div>
<div class="line number22 index21 alt1"><code class="csharp spaces">    </code><code class="csharp plain">AccountHelper.AddCache(systemRole.SystemModules);</code></div>
<div class="line number23 index22 alt2"></div>
<div class="line number24 index23 alt1"><code class="csharp spaces">    </code><code class="csharp keyword">return</code> <code class="csharp plain">RedirectToAction(</code><code class="csharp string">"Index"</code><code class="csharp plain">, </code><code class="csharp string">"Home"</code><code class="csharp plain">);</code></div>
<div class="line number25 index24 alt2"><code class="csharp plain">}</code></div>
<div class="line number25 index24 alt2">
AccountHelper:
</div>
<div class="line number25 index24 alt2">
<div class="container">
<div class="line number1 index0 alt2"><code class="csharp keyword">public</code> <code class="csharp keyword">class</code> <code class="csharp plain">AccountHelper</code></div>
<div class="line number2 index1 alt1"><code class="csharp plain">{</code></div>
<div class="line number3 index2 alt2"><code class="csharp spaces">    </code><code class="csharp keyword">private</code> <code class="csharp keyword">const</code> <code class="csharp keyword">string</code> <code class="csharp plain">CacheName = </code><code class="csharp string">"SystemModules"</code><code class="csharp plain">;</code></div>
<div class="line number4 index3 alt1"></div>
<div class="line number5 index4 alt2"><code class="csharp spaces">    </code><code class="csharp color1">/// &lt;summary&gt;</code></div>
<div class="line number6 index5 alt1"><code class="csharp spaces">    </code><code class="csharp color1">/// 获取用户信息</code></div>
<div class="line number7 index6 alt2"><code class="csharp spaces">    </code><code class="csharp color1">/// &lt;/summary&gt;</code></div>
<div class="line number8 index7 alt1"><code class="csharp spaces">    </code><code class="csharp color1">/// &lt;returns&gt;&lt;/returns&gt;</code></div>
<div class="line number9 index8 alt2"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp keyword">static</code> <code class="csharp plain">FormsAuthenticationTicket GetCookieUser()</code></div>
<div class="line number10 index9 alt1"><code class="csharp spaces">    </code><code class="csharp plain">{</code></div>
<div class="line number11 index10 alt2"><code class="csharp spaces">        </code><code class="csharp plain">HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];</code></div>
<div class="line number12 index11 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">if</code> <code class="csharp plain">(authCookie == </code><code class="csharp keyword">null</code> <code class="csharp plain">|| authCookie.Value == </code><code class="csharp string">""</code><code class="csharp plain">)</code></div>
<div class="line number13 index12 alt2"><code class="csharp spaces">        </code><code class="csharp plain">{</code></div>
<div class="line number14 index13 alt1"><code class="csharp spaces">            </code><code class="csharp keyword">return</code> <code class="csharp keyword">null</code><code class="csharp plain">;</code></div>
<div class="line number15 index14 alt2"><code class="csharp spaces">        </code><code class="csharp plain">}</code></div>
<div class="line number16 index15 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">try</code></div>
<div class="line number17 index16 alt2"><code class="csharp spaces">        </code><code class="csharp plain">{</code></div>
<div class="line number18 index17 alt1"><code class="csharp spaces">            </code><code class="csharp keyword">return</code> <code class="csharp plain">FormsAuthentication.Decrypt(authCookie.Value);</code></div>
<div class="line number19 index18 alt2"><code class="csharp spaces">        </code><code class="csharp plain">}</code></div>
<div class="line number20 index19 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">catch</code> <code class="csharp plain">(Exception ex)</code></div>
<div class="line number21 index20 alt2"><code class="csharp spaces">        </code><code class="csharp plain">{</code></div>
<div class="line number22 index21 alt1"><code class="csharp spaces">            </code><code class="csharp keyword">return</code> <code class="csharp keyword">null</code><code class="csharp plain">;</code></div>
<div class="line number23 index22 alt2"><code class="csharp spaces">        </code><code class="csharp plain">}</code></div>
<div class="line number24 index23 alt1"><code class="csharp spaces">    </code><code class="csharp plain">}</code></div>
<div class="line number25 index24 alt2"></div>
<div class="line number26 index25 alt1"><code class="csharp spaces">    </code><code class="csharp color1">/// &lt;summary&gt;</code></div>
<div class="line number27 index26 alt2"><code class="csharp spaces">    </code><code class="csharp color1">/// 添加缓存</code></div>
<div class="line number28 index27 alt1"><code class="csharp spaces">    </code><code class="csharp color1">/// &lt;/summary&gt;</code></div>
<div class="line number29 index28 alt2"><code class="csharp spaces">    </code><code class="csharp color1">/// &lt;param name="systemModules"&gt;&lt;/param&gt;</code></div>
<div class="line number30 index29 alt1"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp keyword">static</code> <code class="csharp keyword">void</code> <code class="csharp plain">AddCache(List&lt;SystemModule&gt; systemModules)</code></div>
<div class="line number31 index30 alt2"><code class="csharp spaces">    </code><code class="csharp plain">{</code></div>
<div class="line number32 index31 alt1"><code class="csharp spaces">        </code><code class="csharp plain">HttpContext.Current.Cache[CacheName] = systemModules;</code></div>
<div class="line number33 index32 alt2"><code class="csharp spaces">    </code><code class="csharp plain">}</code></div>
<div class="line number34 index33 alt1"></div>
<div class="line number35 index34 alt2"><code class="csharp spaces">    </code><code class="csharp color1">/// &lt;summary&gt;</code></div>
<div class="line number36 index35 alt1"><code class="csharp spaces">    </code><code class="csharp color1">/// 读取缓存</code></div>
<div class="line number37 index36 alt2"><code class="csharp spaces">    </code><code class="csharp color1">/// &lt;/summary&gt;</code></div>
<div class="line number38 index37 alt1"><code class="csharp spaces">    </code><code class="csharp color1">/// &lt;returns&gt;&lt;/returns&gt;</code></div>
<div class="line number39 index38 alt2"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp keyword">static</code> <code class="csharp plain">List&lt;SystemModule&gt; GetCache()</code></div>
<div class="line number40 index39 alt1"><code class="csharp spaces">    </code><code class="csharp plain">{</code></div>
<div class="line number41 index40 alt2"><code class="csharp spaces">        </code><code class="csharp keyword">if</code> <code class="csharp plain">(HttpContext.Current.Cache[CacheName] == </code><code class="csharp keyword">null</code><code class="csharp plain">)</code></div>
<div class="line number42 index41 alt1"><code class="csharp spaces">        </code><code class="csharp plain">{</code></div>
<div class="line number43 index42 alt2"><code class="csharp spaces">            </code><code class="csharp comments">// 重新构建权限</code></div>
<div class="line number44 index43 alt1"><code class="csharp spaces">            </code><code class="csharp keyword">var</code> <code class="csharp plain">user = GetCookieUser();</code></div>
<div class="line number45 index44 alt2"><code class="csharp spaces">            </code><code class="csharp keyword">var</code> <code class="csharp plain">roles = user.UserData.Split(</code><code class="csharp keyword">new</code><code class="csharp plain">[] { </code><code class="csharp string">','</code> <code class="csharp plain">});</code></div>
<div class="line number46 index45 alt1"><code class="csharp spaces">            </code><code class="csharp plain">HttpContext.Current.Cache[CacheName] = SystemRole.Init(roles).SystemModules;</code></div>
<div class="line number47 index46 alt2"><code class="csharp spaces">        </code><code class="csharp plain">}</code></div>
<div class="line number48 index47 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">return</code> <code class="csharp plain">(List&lt;SystemModule&gt;)HttpContext.Current.Cache[CacheName];</code></div>
<div class="line number49 index48 alt2"><code class="csharp spaces">    </code><code class="csharp plain">}</code></div>
<div class="line number50 index49 alt1"></div>
<div class="line number51 index50 alt2"><code class="csharp spaces">    </code><code class="csharp color1">/// &lt;summary&gt;</code></div>
<div class="line number52 index51 alt1"><code class="csharp spaces">    </code><code class="csharp color1">/// 验证Controller和Action</code></div>
<div class="line number53 index52 alt2"><code class="csharp spaces">    </code><code class="csharp color1">/// &lt;/summary&gt;</code></div>
<div class="line number54 index53 alt1"><code class="csharp spaces">    </code><code class="csharp color1">/// &lt;param name="controllerName"&gt;&lt;/param&gt;</code></div>
<div class="line number55 index54 alt2"><code class="csharp spaces">    </code><code class="csharp color1">/// &lt;param name="actionName"&gt;&lt;/param&gt;</code></div>
<div class="line number56 index55 alt1"><code class="csharp spaces">    </code><code class="csharp color1">/// &lt;returns&gt;&lt;/returns&gt;</code></div>
<div class="line number57 index56 alt2"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp keyword">static</code> <code class="csharp keyword">bool</code> <code class="csharp plain">ValidatePermission(</code><code class="csharp keyword">string</code> <code class="csharp plain">controllerName, </code><code class="csharp keyword">string</code> <code class="csharp plain">actionName)</code></div>
<div class="line number58 index57 alt1"><code class="csharp spaces">    </code><code class="csharp plain">{</code></div>
<div class="line number59 index58 alt2"><code class="csharp spaces">        </code><code class="csharp keyword">var</code> <code class="csharp plain">systemModules = GetCache();</code></div>
<div class="line number60 index59 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">foreach</code> <code class="csharp plain">(</code><code class="csharp keyword">var</code> <code class="csharp plain">systemModule </code><code class="csharp keyword">in</code> <code class="csharp plain">systemModules)</code></div>
<div class="line number61 index60 alt2"><code class="csharp spaces">        </code><code class="csharp plain">{</code></div>
<div class="line number62 index61 alt1"><code class="csharp spaces">            </code><code class="csharp keyword">if</code> <code class="csharp plain">(systemModule != </code><code class="csharp keyword">null</code> <code class="csharp plain">&amp;&amp; systemModule.SystemModuleControllers != </code><code class="csharp keyword">null</code><code class="csharp plain">)</code></div>
<div class="line number63 index62 alt2"><code class="csharp spaces">            </code><code class="csharp plain">{</code></div>
<div class="line number64 index63 alt1"><code class="csharp spaces">                </code><code class="csharp keyword">foreach</code> <code class="csharp plain">(</code><code class="csharp keyword">var</code> <code class="csharp plain">controller </code><code class="csharp keyword">in</code> <code class="csharp plain">systemModule.SystemModuleControllers)</code></div>
<div class="line number65 index64 alt2"><code class="csharp spaces">                </code><code class="csharp plain">{</code></div>
<div class="line number66 index65 alt1"><code class="csharp spaces">                    </code><code class="csharp keyword">if</code> <code class="csharp plain">(controller.ControllerName == controllerName &amp;&amp; controller.ActionName == actionName) </code><code class="csharp keyword">return</code> <code class="csharp keyword">true</code><code class="csharp plain">;</code></div>
<div class="line number67 index66 alt2"><code class="csharp spaces">                </code><code class="csharp plain">}</code></div>
<div class="line number68 index67 alt1"><code class="csharp spaces">            </code><code class="csharp plain">}</code></div>
<div class="line number69 index68 alt2"><code class="csharp spaces">        </code><code class="csharp plain">}</code></div>
<div class="line number70 index69 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">return</code> <code class="csharp keyword">false</code><code class="csharp plain">;</code></div>
<div class="line number71 index70 alt2"><code class="csharp spaces">    </code><code class="csharp plain">}</code></div>
<div class="line number72 index71 alt1"><code class="csharp plain">}</code></div>
<div class="line number72 index71 alt1">
同样在业务的Controller添加拦截标记
</div>
<div class="line number72 index71 alt1">
<div class="container">
<div class="line number1 index0 alt2"><code class="csharp plain">[LoginAllow]</code></div>
<div class="line number2 index1 alt1"><code class="csharp plain">[PermissionFilter]</code></div>
<div class="line number3 index2 alt2"><code class="csharp keyword">public</code> <code class="csharp keyword">class</code> <code class="csharp plain">BaseController : Controller</code></div>
<div class="line number4 index3 alt1"><code class="csharp plain">{</code></div>
<div class="line number5 index4 alt2"><code class="csharp plain">}</code></div>
<div class="line number6 index5 alt1"></div>
<div class="line number7 index6 alt2"><code class="csharp plain">[Description(</code><code class="csharp string">"规划管理控制器"</code><code class="csharp plain">)]</code></div>
<div class="line number8 index7 alt1"><code class="csharp plain">[ModuleAuthorization]</code></div>
<div class="line number9 index8 alt2"><code class="csharp keyword">public</code> <code class="csharp keyword">class</code> <code class="csharp plain">PlanManagementController : BaseController</code></div>
<div class="line number10 index9 alt1"><code class="csharp plain">{</code></div>
<div class="line number11 index10 alt2"><code class="csharp spaces">    </code><code class="csharp plain">[Description(</code><code class="csharp string">"首页"</code><code class="csharp plain">)]</code></div>
<div class="line number12 index11 alt1"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp plain">ActionResult Index()</code></div>
<div class="line number13 index12 alt2"><code class="csharp spaces">    </code><code class="csharp plain">{</code></div>
<div class="line number14 index13 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">return</code> <code class="csharp plain">View();</code></div>
<div class="line number15 index14 alt2"><code class="csharp spaces">    </code><code class="csharp plain">}</code></div>
<div class="line number16 index15 alt1"></div>
<div class="line number17 index16 alt2"><code class="csharp spaces">    </code><code class="csharp plain">[Description(</code><code class="csharp string">"查询"</code><code class="csharp plain">)]</code></div>
<div class="line number18 index17 alt1"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp plain">ActionResult Search()</code></div>
<div class="line number19 index18 alt2"><code class="csharp spaces">    </code><code class="csharp plain">{</code></div>
<div class="line number20 index19 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">return</code> <code class="csharp plain">View();</code></div>
<div class="line number21 index20 alt2"><code class="csharp spaces">    </code><code class="csharp plain">}</code></div>
<div class="line number22 index21 alt1"></div>
<div class="line number23 index22 alt2"><code class="csharp spaces">    </code><code class="csharp plain">[Description(</code><code class="csharp string">"添加"</code><code class="csharp plain">)]</code></div>
<div class="line number24 index23 alt1"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp plain">ActionResult Add()</code></div>
<div class="line number25 index24 alt2"><code class="csharp spaces">    </code><code class="csharp plain">{</code></div>
<div class="line number26 index25 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">return</code> <code class="csharp plain">View();</code></div>
<div class="line number27 index26 alt2"><code class="csharp spaces">    </code><code class="csharp plain">}</code></div>
<div class="line number28 index27 alt1"></div>
<div class="line number29 index28 alt2"><code class="csharp spaces">    </code><code class="csharp plain">[Description(</code><code class="csharp string">"编辑"</code><code class="csharp plain">)]</code></div>
<div class="line number30 index29 alt1"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp plain">ActionResult Edit()</code></div>
<div class="line number31 index30 alt2"><code class="csharp spaces">    </code><code class="csharp plain">{</code></div>
<div class="line number32 index31 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">return</code> <code class="csharp plain">View();</code></div>
<div class="line number33 index32 alt2"><code class="csharp spaces">    </code><code class="csharp plain">}</code></div>
<div class="line number34 index33 alt1"></div>
<div class="line number35 index34 alt2"><code class="csharp spaces">    </code><code class="csharp plain">[Description(</code><code class="csharp string">"删除"</code><code class="csharp plain">)]</code></div>
<div class="line number36 index35 alt1"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp plain">ActionResult Delete()</code></div>
<div class="line number37 index36 alt2"><code class="csharp spaces">    </code><code class="csharp plain">{</code></div>
<div class="line number38 index37 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">return</code> <code class="csharp plain">View();</code></div>
<div class="line number39 index38 alt2"><code class="csharp spaces">    </code><code class="csharp plain">}</code></div>
<div class="line number40 index39 alt1"></div>
<div class="line number41 index40 alt2"><code class="csharp spaces">    </code><code class="csharp plain">[Description(</code><code class="csharp string">"审批"</code><code class="csharp plain">)]</code></div>
<div class="line number42 index41 alt1"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp plain">ActionResult Approval()</code></div>
<div class="line number43 index42 alt2"><code class="csharp spaces">    </code><code class="csharp plain">{</code></div>
<div class="line number44 index43 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">return</code> <code class="csharp plain">View();</code></div>
<div class="line number45 index44 alt2"><code class="csharp spaces">    </code><code class="csharp plain">}</code></div>
<div class="line number46 index45 alt1"><code class="csharp plain">}</code></div>
<div class="line number46 index45 alt1">
拦截器:PermissionFilterAttribute
</div>
<div class="line number46 index45 alt1">
<div class="container">
<div class="line number1 index0 alt2"><code class="csharp plain">[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = </code><code class="csharp keyword">false</code><code class="csharp plain">)]</code></div>
<div class="line number2 index1 alt1"><code class="csharp spaces">  </code><code class="csharp keyword">public</code> <code class="csharp keyword">class</code> <code class="csharp plain">PermissionFilterAttribute : ActionFilterAttribute</code></div>
<div class="line number3 index2 alt2"><code class="csharp spaces">  </code><code class="csharp plain">{</code></div>
<div class="line number4 index3 alt1"><code class="csharp spaces">      </code><code class="csharp comments">// OnActionExecuted 在执行操作方法后由 ASP.NET MVC 框架调用。</code></div>
<div class="line number5 index4 alt2"><code class="csharp spaces">      </code><code class="csharp comments">// OnActionExecuting 在执行操作方法之前由 ASP.NET MVC 框架调用。</code></div>
<div class="line number6 index5 alt1"><code class="csharp spaces">      </code><code class="csharp comments">// OnResultExecuted 在执行操作结果后由 ASP.NET MVC 框架调用。</code></div>
<div class="line number7 index6 alt2"><code class="csharp spaces">      </code><code class="csharp comments">// OnResultExecuting 在执行操作结果之前由 ASP.NET MVC 框架调用。</code></div>
<div class="line number8 index7 alt1"></div>
<div class="line number9 index8 alt2"></div>
<div class="line number10 index9 alt1"><code class="csharp spaces">      </code><code class="csharp color1">/// &lt;summary&gt;</code></div>
<div class="line number11 index10 alt2"><code class="csharp spaces">      </code><code class="csharp color1">/// 在执行操作方法之前由 ASP.NET MVC 框架调用。</code></div>
<div class="line number12 index11 alt1"><code class="csharp spaces">      </code><code class="csharp color1">/// &lt;/summary&gt;</code></div>
<div class="line number13 index12 alt2"><code class="csharp spaces">      </code><code class="csharp color1">/// &lt;param name="filterContext"&gt;&lt;/param&gt;</code></div>
<div class="line number14 index13 alt1"><code class="csharp spaces">      </code><code class="csharp keyword">public</code> <code class="csharp keyword">override</code> <code class="csharp keyword">void</code> <code class="csharp plain">OnActionExecuting(ActionExecutingContext filterContext)</code></div>
<div class="line number15 index14 alt2"><code class="csharp spaces">      </code><code class="csharp plain">{</code></div>
<div class="line number16 index15 alt1"><code class="csharp spaces">          </code><code class="csharp comments">//fcinfo = new filterContextInfo(filterContext);</code></div>
<div class="line number17 index16 alt2"></div>
<div class="line number18 index17 alt1"><code class="csharp spaces">          </code><code class="csharp comments">//根据验证判断进行处理</code></div>
<div class="line number19 index18 alt2"><code class="csharp spaces">          </code><code class="csharp keyword">if</code> <code class="csharp plain">(!</code><code class="csharp keyword">this</code><code class="csharp plain">.AuthorizeCore(filterContext))</code></div>
<div class="line number20 index19 alt1"><code class="csharp spaces">          </code><code class="csharp plain">{</code></div>
<div class="line number21 index20 alt2"><code class="csharp spaces">              </code><code class="csharp plain">filterContext.RequestContext.HttpContext.Response.Redirect(</code><code class="csharp string">"~/Account/Login"</code><code class="csharp plain">);</code></div>
<div class="line number22 index21 alt1"><code class="csharp spaces">          </code><code class="csharp plain">}</code></div>
<div class="line number23 index22 alt2"><code class="csharp spaces">      </code><code class="csharp plain">}</code></div>
<div class="line number24 index23 alt1"></div>
<div class="line number25 index24 alt2"><code class="csharp spaces">      </code><code class="csharp color1">/// &lt;summary&gt;</code></div>
<div class="line number26 index25 alt1"><code class="csharp spaces">      </code><code class="csharp color1">/// //权限判断业务逻辑</code></div>
<div class="line number27 index26 alt2"><code class="csharp spaces">      </code><code class="csharp color1">/// &lt;/summary&gt;</code></div>
<div class="line number28 index27 alt1"><code class="csharp spaces">      </code><code class="csharp color1">/// &lt;param name="filterContext"&gt;&lt;/param&gt;</code></div>
<div class="line number29 index28 alt2"><code class="csharp spaces">      </code><code class="csharp color1">/// &lt;returns&gt;&lt;/returns&gt;</code></div>
<div class="line number30 index29 alt1"><code class="csharp spaces">      </code><code class="csharp keyword">protected</code> <code class="csharp keyword">virtual</code> <code class="csharp keyword">bool</code> <code class="csharp plain">AuthorizeCore(ActionExecutingContext filterContext)</code></div>
<div class="line number31 index30 alt2"><code class="csharp spaces">      </code><code class="csharp plain">{</code></div>
<div class="line number32 index31 alt1"><code class="csharp spaces">          </code><code class="csharp keyword">object</code><code class="csharp plain">[] filter;</code></div>
<div class="line number33 index32 alt2"></div>
<div class="line number34 index33 alt1"><code class="csharp spaces">          </code><code class="csharp comments">// 验证当前Action是否是匿名访问Action</code></div>
<div class="line number35 index34 alt2"><code class="csharp spaces">          </code><code class="csharp plain">filter = filterContext.Controller.GetType().GetCustomAttributes(</code><code class="csharp keyword">typeof</code><code class="csharp plain">(AnonymousAttribute), </code><code class="csharp keyword">true</code><code class="csharp plain">);</code></div>
<div class="line number36 index35 alt1"><code class="csharp spaces">          </code><code class="csharp keyword">if</code> <code class="csharp plain">(filter.Length == 1)</code></div>
<div class="line number37 index36 alt2"><code class="csharp spaces">          </code><code class="csharp plain">{</code></div>
<div class="line number38 index37 alt1"><code class="csharp spaces">              </code><code class="csharp keyword">return</code> <code class="csharp keyword">true</code><code class="csharp plain">;</code></div>
<div class="line number39 index38 alt2"><code class="csharp spaces">          </code><code class="csharp plain">}</code></div>
<div class="line number40 index39 alt1"></div>
<div class="line number41 index40 alt2"><code class="csharp spaces">          </code><code class="csharp comments">// 验证当前Action是否是权限控制页面Action</code></div>
<div class="line number42 index41 alt1"><code class="csharp spaces">          </code><code class="csharp plain">filter = filterContext.Controller.GetType().GetCustomAttributes(</code><code class="csharp keyword">typeof</code><code class="csharp plain">(ModuleAuthorizationAttribute), </code><code class="csharp keyword">true</code><code class="csharp plain">);</code></div>
<div class="line number43 index42 alt2"><code class="csharp spaces">          </code><code class="csharp keyword">if</code> <code class="csharp plain">(filter.Length == 1)</code></div>
<div class="line number44 index43 alt1"><code class="csharp spaces">          </code><code class="csharp plain">{</code></div>
<div class="line number45 index44 alt2"><code class="csharp spaces">              </code><code class="csharp comments">//获取 controllerName 名称</code></div>
<div class="line number46 index45 alt1"><code class="csharp spaces">              </code><code class="csharp keyword">var</code> <code class="csharp plain">controllerName = filterContext.RouteData.Values[</code><code class="csharp string">"controller"</code><code class="csharp plain">].ToString();</code></div>
<div class="line number47 index46 alt2"><code class="csharp spaces">              </code><code class="csharp comments">//获取ACTION 名称</code></div>
<div class="line number48 index47 alt1"><code class="csharp spaces">              </code><code class="csharp keyword">var</code> <code class="csharp plain">actionName = filterContext.RouteData.Values[</code><code class="csharp string">"action"</code><code class="csharp plain">].ToString();</code></div>
<div class="line number49 index48 alt2"><code class="csharp spaces">              </code><code class="csharp keyword">return</code> <code class="csharp plain">AccountHelper.ValidatePermission(controllerName, actionName);</code></div>
<div class="line number50 index49 alt1"><code class="csharp spaces">          </code><code class="csharp plain">}</code></div>
<div class="line number51 index50 alt2"></div>
<div class="line number52 index51 alt1"><code class="csharp spaces">          </code><code class="csharp comments">// 验证当前Action是否是登录用户Action</code></div>
<div class="line number53 index52 alt2"><code class="csharp spaces">          </code><code class="csharp plain">filter = filterContext.Controller.GetType().GetCustomAttributes(</code><code class="csharp keyword">typeof</code><code class="csharp plain">(LoginAllowAttribute), </code><code class="csharp keyword">true</code><code class="csharp plain">);</code></div>
<div class="line number54 index53 alt1"><code class="csharp spaces">          </code><code class="csharp keyword">if</code> <code class="csharp plain">(filter.Length == 1)</code></div>
<div class="line number55 index54 alt2"><code class="csharp spaces">          </code><code class="csharp plain">{</code></div>
<div class="line number56 index55 alt1"><code class="csharp spaces">              </code><code class="csharp keyword">return</code> <code class="csharp plain">HttpContext.Current.User.Identity.IsAuthenticated;</code></div>
<div class="line number57 index56 alt2"><code class="csharp spaces">          </code><code class="csharp plain">}</code></div>
<div class="line number58 index57 alt1"></div>
<div class="line number59 index58 alt2"><code class="csharp spaces">          </code><code class="csharp keyword">throw</code> <code class="csharp keyword">new</code> <code class="csharp plain">Exception(</code><code class="csharp string">"用户验证失败!"</code><code class="csharp plain">);</code></div>
<div class="line number60 index59 alt1"><code class="csharp spaces">      </code><code class="csharp plain">}</code></div>
<div class="line number61 index60 alt2"><code class="csharp spaces">  </code><code class="csharp plain">}</code></div>
<div class="line number61 index60 alt2">

 

代码下载:PRMMS.Authorization.zip

赞(0) 打赏
分享到: 更多 (0)
标签:

相关推荐

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏