• View Based Authorization 基于视图的授权

    44 of 46 people found this helpful

    Often 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 @inject directive, for example @inject IAuthorizationService AuthorizationService. If you want the authorization service in every view then place the @inject directive into the _ViewImports.cshtml file in the Views directory. 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 AuthorizeAsync method 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 AuthorizeAsync in 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 » 基于视图的授权的更多相关文章

  1. Security » Authorization » 基于资源的授权

    Resource Based Authorization¶ 基于资源的授权 68 of 73 people found this helpful Often authorization depends ...

  2. Security » Authorization » 基于声明的授权

    Claims-Based Authorization¶ 基于声明的授权 142 of 162 people found this helpful When an identity is created ...

  3. Security » Authorization » 基于角色的授权

    Role based Authorization¶ 基于角色的授权 133 of 153 people found this helpful When an identity is created i ...

  4. Security » Authorization » 基于自定义策略的授权

    Custom Policy-Based Authorization¶ 基于自定义策略的授权 98 of 108 people found this helpful Underneath the cov ...

  5. ASP.NET MVC 随想录—— 使用ASP.NET Identity实现基于声明的授权,高级篇

    在这篇文章中,我将继续ASP.NET Identity 之旅,这也是ASP.NET Identity 三部曲的最后一篇.在本文中,将为大家介绍ASP.NET Identity 的高级功能,它支持声明式 ...

  6. Security » Authorization » 通过映射限制身份

    Limiting identity by scheme¶ 通过映射限制身份(这部分有好几个概念还不清楚,翻译的有问题) 36 of 39 people found this helpful In so ...

  7. ASP.NET MVC 随想录——探索ASP.NET Identity 身份验证和基于角色的授权,中级篇

    在前一篇文章中,我介绍了ASP.NET Identity 基本API的运用并创建了若干用户账号.那么在本篇文章中,我将继续ASP.NET Identity 之旅,向您展示如何运用ASP.NET Ide ...

  8. Microsoft.Owin.Security.OAuth搭建OAuth2.0授权服务端

    Microsoft.Owin.Security.OAuth搭建OAuth2.0授权服务端 目录 前言 OAuth2.0简介 授权模式 (SimpleSSO示例) 使用Microsoft.Owin.Se ...

  9. ASP.NET Identity 身份验证和基于角色的授权

    ASP.NET Identity 身份验证和基于角色的授权 阅读目录 探索身份验证与授权 使用ASP.NET Identity 身份验证 使用角色进行授权 初始化数据,Seeding 数据库 小结 在 ...

随机推荐

  1. Qt之添加QLabel的点击事件

    QLabel功能为显示了一个字符串或者图片等信息,它本身没有click信号.也就不能够响应click点击事件,有什么办法来实现来,我们可以子类化QLabel,实现MouseXXXEvent.class ...

  2. (二)sql入门 管理数据库对象

    在数据库里,有各种各样的对象,除了最常见的表之外,还有诸如视图.索引等数据库对象. 这些对象,在数据库里需要有人来管理,那么谁来管理呢?当然是数据库的使用者了.每个使用者相对于数据库里有一片区域,称为 ...

  3. PHP 生成验证码

    //加载 vendor目录的phpqrcode.类文件 $a = vendor("phpqrcode.qrlib");// 创建目录 echo mkdir(__ROOT__.'/U ...

  4. iOS tableView 滚动后回到顶部

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGPoint contentOffsetPoint = self.tableView ...

  5. Terra Vista 6.2

    最近在做虚拟仿真相关工作,想把GIS中的一些想法用虚拟显示技术实现,在保证准确性的同时,提高展现效果. 前不久在朋友圈获得了一个强大的三维地形构建软件Terra Vista 6.2,据说这个软件是加拿 ...

  6. bootstrap——下拉菜单右对齐

    通过向 .dropdown-menu 添加 .pull-right 类来向右对齐下拉菜单. Bootstrap默认情况下,下拉菜单自动沿着父元素的上沿和左侧被定位为 100% 宽度. 为 .dropd ...

  7. xml 的 CDATA

    <PolicyURL><![CDATA[http://ectp.tpi.cntaiping.com/TPEBizWeb/pages/B2C/show/downLoad.do?poli ...

  8. c++之变量的引用

    昨天耀哥在问引用的时候,当时有一点点懵.平时经常用到引用的时候就是函数的参数传递. 引用就是某一变量的一个别名,对引用的操作与对变量直接操作完全一样,但是要注意到的是引用并没有新开辟内存空间,因此并不 ...

  9. linux下打开、关闭tomcat,实时查看tomcat运行日志

    启动:一般是执行sh tomcat/bin/startup.sh 停止:一般是执行sh tomcat/bin/shutdown.sh脚本命令 查看:执行ps -ef |grep tomcat 输出如下 ...

  10. 阻止iOS中页面弹性回滚,只允许div.phone_body的区块有弹性

    使用说明:只要替换选择器:var selector = '.phone_body'; /** * 阻止iOS中页面弹性回滚,只允许div.scroller的区块有弹性 */ (function () ...