• 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. mybatis教程

    http://www.yihaomen.com/article/java/302.htm

  2. JAVA 学习随笔 : JDK Enhancement Process JEP process

    是时候寻找一个学习JAVA的路径了 ---- JDK Enhancement Process Oracle发布了JDK增强提案与路线图进程,目的在于鼓励OpenJDK提交者贡献点子和扩展以改进Open ...

  3. Unable to execute dex: GC overhead limit exceeded

    Android打包时下面的错误: Unable to execute dex: GC overhead limit exceeded GC overhead limit exceeded 解决的方法: ...

  4. make命令以及makefile

    make命令以及makefile使用RCS与CVS进行源代码控制编写手册页使用patch与tar发布软件开发环境 多源代码的问题 当我们编写小程序时,许多人都是简单的在编辑后通过重新编译所有的文件重新 ...

  5. CoreData 轻量级迁移

    CoreData 轻量级迁移 Core Data 的轻量级迁移可以处理对数据模型进行的简单更改,例如将新属性添加到现有实体中,轻量级迁移基本上与普通迁移相同,区别就是不需要映射模型,因为Core Da ...

  6. quick-cocos2d-x :加入精灵背景

    最近几天都在学习 quick  找例子学习. 一直也没什么好的. 并且 还不会 lua .学习学习 突突突 官方文档上的打地鼠 都没了. 不知道为什么链接不好使. 好吧 那么今天就先做一个简单的 例子 ...

  7. 乐1S 5.8(Android 6.0) 刷第三方recovery并刷入root权限

    说明 我的是 乐视1S, 系统为EUI 5.8 (powered by Android 6.0) 使用的是 superSu来root, 你可以到这里下载 文件 *下载 Recovery Flashab ...

  8. HTML、JavaScript之单双引号转义

    一.HTML : 双引号:" 单引号:&apos; 二.JavaScript: 双引号:\" 单引号:\'

  9. 放在NSArray、NSDictionary等容器内的对象Item,Item中的property在程序运行过程中被无故释放

    可能是被释放的property本身是OC对象而它的属性被误写成assign,例如: @interface MyItem : Object @property (nonatomic, assign) N ...

  10. [译]Dynamics AX 2012 R2 BI系列-规划分析的注意事项

    https://msdn.microsoft.com/en-us/library/gg731898.aspx       在开始实施AX的分析特性前,有很多事情要考虑.本文描述了你必须考虑的事情,和在 ...