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 数据库 小结 在 ...
随机推荐
- html5 webDatabase 存储中sql语句执行可嵌套使用
html5 webDatabase 存储中sql语句执行可嵌套使用,代码如下: *); data.transaction(function(tx){ tx.executeSql("creat ...
- Windows内核开发中如何区分文件对象究竟是文件还是文件夹?
今天有同行问了一个问题,Windows文件过滤驱动里的如何去区分一个对象是文件还是文件夹?我花了1小时左右翻阅了一些微软的文档以及以前的遗留代码,发现在WDK的帮助文档中是这么定义的: FILE_OB ...
- How to install flashplugin on ubuntu
sudo apt-get install flashplugin-installer
- iOS解析XML数据
iOS中解析XML数据的类是 NSXMLParser,详细使用方法如下: 假设现在在内存中有XML的二进制数据对象(NSData):data(该数据可能来自网络,也可能是本地的文件数据),设置NSX ...
- 8139too.c网卡驱动简单分析
从事linux C开发工作以来,工作内容主要是在应用层,对nginx和unbound等软件有些了解,也常对这2个软件进行二次开发. 对网络这块一直比较有兴趣.也很好奇网卡到底是怎么接受到报文的,以及报 ...
- PHP基础班初学心得:用JQ实现表单的全选、反选、取消和删除功能
摘要: 本人刚参加PHP基础班培训,由于之前毫无基础,分享的心得可能不规范,方法也许也"旁门左道",不能保证质量,只作自己总结学习,也希望能帮助到同样是初学者的朋友们,共同进步. ...
- 【实践】js 如何实现动态添加文本节点
对于我这个js 小白来说 今天鼓起勇气做起了邮箱认证这个特效 但是这次不是想说如何实现这这个特效而是想记录一下特效当中的某个部分 那就是向元素节点动态添加文本节点 百度了一下动态添加文本节点的方式 是 ...
- FTP连接时出现“227 Entering Passive Mode” 的解决方法
今天从公网的服务器连接本地内网的FTP server copy文件时,系统老是提示227 Entering Passive Mode (xxx,xxx,,xxx,xxx,x),很是奇怪,于是上网找资料 ...
- OC-@property、self及类的本质
让代码书写更加简便 --1-- 设置器和访问器 1.1 setter 1.2 getter --2-- 类的本质 2.1 类类型的对象 2.2 类的本质 2.3 如何获取类对象 2.4 类对象的使用 ...
- Uva 11354 LCA 倍增祖先
题目链接:https://vjudge.net/contest/144221#problem/B 题意:找一条从 s 到 t 的路,使得瓶颈路最小. 点的数目是10^4,如果向之前的方案求 maxc ...