Security » Authorization » 基于视图的授权
View Based Authorization¶ 基于视图的授权
44 of 46 people found this helpfulOften a developer will want to show, hide or otherwise modify a UI based on the current user identity. You can access the authorization service within MVC views via dependency injection. To inject the authorization service into a Razor view use the
@injectdirective, for example@inject IAuthorizationService AuthorizationService. If you want the authorization service in every view then place the@injectdirective into the_ViewImports.cshtmlfile in theViewsdirectory. For more information on dependency injection into views see Dependency injection into views.- 开发者经常需要根据当前用户的身份来确定是否展示、隐藏或者是否修改一个页面。你可以通过依赖注入在MVC的视图中使用授权服务。要将授权服务注入Razor视图就要使用@inject 命令,例如@inject IAuthorizationService AuthorizationService。 如果想在左右视图中使用授权服务,就把
@inject命令放置于Views目录内的_ViewImports.cshtml文件中。 更多的依赖注入到视图的信息,请参看Dependency injection into views。 Once you have injected the authorization service you use it by calling the
AuthorizeAsyncmethod in exactly the same way as you would check during resource based authorization.一旦将授权服务注入后,就可通过调用
AuthorizeAsync方法来使用了,就和resource based authorization(基于资源的授权)中进行的检查一模一样。@if (await AuthorizationService.AuthorizeAsync(User, "PolicyName"))
{
<p>This paragraph is displayed because you fulfilled PolicyName.</p>
}In some cases the resource will be your view model, and you can call
AuthorizeAsyncin exactly the same way as you would check during resource based authorization;在某些情况下,资源就是你的视图模型,可通过与resource based authorization(基于资源的授权)中一样的方法来调用
AuthorizeAsync。@if (await AuthorizationService.AuthorizeAsync(User, Model, Operations.Edit))
{
<p><a class="btn btn-default" role="button"
href="@Url.Action("Edit", "Document", new { id = Model.Id })">Edit</a></p>
}Here you can see the model is passed as the resource authorization should take into consideration.
在这里,您可以看到该模型被作为资源授权而传递了。
Warning 注意
Do not rely on showing or hiding parts of your UI as your only authorization method. Hiding a UI element does not mean a user cannot access it. You must also authorize the user within your controller code.
不要依赖于显示或隐藏你的用户界面的部分作为你唯一的授权方法。隐藏一个用户界面元素并不意味着用户无法访问它。您还必须授权您的控制器代码中的用户。
Security » Authorization » 基于视图的授权的更多相关文章
- Security » Authorization » 基于资源的授权
Resource Based Authorization¶ 基于资源的授权 68 of 73 people found this helpful Often authorization depends ...
- Security » Authorization » 基于声明的授权
Claims-Based Authorization¶ 基于声明的授权 142 of 162 people found this helpful When an identity is created ...
- Security » Authorization » 基于角色的授权
Role based Authorization¶ 基于角色的授权 133 of 153 people found this helpful When an identity is created i ...
- Security » Authorization » 基于自定义策略的授权
Custom Policy-Based Authorization¶ 基于自定义策略的授权 98 of 108 people found this helpful Underneath the cov ...
- ASP.NET MVC 随想录—— 使用ASP.NET Identity实现基于声明的授权,高级篇
在这篇文章中,我将继续ASP.NET Identity 之旅,这也是ASP.NET Identity 三部曲的最后一篇.在本文中,将为大家介绍ASP.NET Identity 的高级功能,它支持声明式 ...
- Security » Authorization » 通过映射限制身份
Limiting identity by scheme¶ 通过映射限制身份(这部分有好几个概念还不清楚,翻译的有问题) 36 of 39 people found this helpful In so ...
- ASP.NET MVC 随想录——探索ASP.NET Identity 身份验证和基于角色的授权,中级篇
在前一篇文章中,我介绍了ASP.NET Identity 基本API的运用并创建了若干用户账号.那么在本篇文章中,我将继续ASP.NET Identity 之旅,向您展示如何运用ASP.NET Ide ...
- Microsoft.Owin.Security.OAuth搭建OAuth2.0授权服务端
Microsoft.Owin.Security.OAuth搭建OAuth2.0授权服务端 目录 前言 OAuth2.0简介 授权模式 (SimpleSSO示例) 使用Microsoft.Owin.Se ...
- ASP.NET Identity 身份验证和基于角色的授权
ASP.NET Identity 身份验证和基于角色的授权 阅读目录 探索身份验证与授权 使用ASP.NET Identity 身份验证 使用角色进行授权 初始化数据,Seeding 数据库 小结 在 ...
随机推荐
- 利用lambda和Collection.forEach
2.外部VS内部迭代 以前Java集合是不能够表达内部迭代的,而只提供了一种外部迭代的方式,也就是for或者while循环. 1 2 3 4 List persons = asList(new Per ...
- hsql数据库使用详解(入门)及快速使用
一.简介: hsql数据库是一款纯Java编写的免费数据库,许可是BSD-style的协议,如果你是使用Java编程的话,不凡考虑一下使用它,相对其 他数据库来说,其体积小,才563kb.仅一个hsq ...
- ThinkPHP的异步搜索
因为公司的后台框架采用了Ajax异步处理,控制器的方法,有时候会被多个连接所重复调用,虽然这个很符合OOP开发思想,但是为了维护这个框架,付出的汗水也是很大的. 说下正题了: 我在后台的搜索框调用了优 ...
- Spring透过ApplicationListener来触发contextrefreshedevent事件
Spring通过ApplicationListener接口来触发contextrefreshedevent事件在开发时有时候需要在整个应用开始运行时执行一些特定代码,比如初始化环境,准备测试数据.加载 ...
- Sublime Text 3 高效编码快捷键
Sublime Text 3 高效编码快捷键 1.快速跳到第20行 Ctrl+p 框中输入 “ :20 ” 2.在文件夹中查看文件 Ctrl+p 框中输入 “ index.html” 更快 ...
- 关于APK签名的一些东西
什么是APK 了解APK签名之前,首先要知道什么是apk文件:APK是AndroidPackage的缩写,即Android安装包(apk),APK文件其实就是zip格式的文件,只是后缀被改为了apk, ...
- BlogEngine2.9模仿yahoo滚动新闻Widget
widget.ascx <%@ Control Language="C#" AutoEventWireup="true" CodeFile="w ...
- crm软件如何实现企业智能化管理?
互联网技术的发展,让企业越来越重视客户的感知体验,企业只有适应并接受再逐步转向智能化发展模式,才能在市场竞争中取胜不被淘汰.选择一款适宜企业自身发展的CRM系统,根据自身的具体情况随需而定,企业才能更 ...
- C#调用RAR压缩与解压
public void RARsave(string rarPatch, string rarFiles,string patch,string rarName) { ...
- python-进程,线程,协程
1.进程和线程 进程定义:进程是正在运行的程序的实例,进程是内核分配资源的最基本的单元,而线程是内核执行的最基本单元,进程内可以包含多个线程,只要记住这三个要点,就可以很清楚的理清进程和线程的行为模 ...