在Asp.Net Core的Razor页面中检查登录用户AuthorizePolicy [英] Checking login user AuthorizePolicy in Razor page on Asp.Net Core

查看:923
本文介绍了在Asp.Net Core的Razor页面中检查登录用户AuthorizePolicy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找这个的变体

@if (SignInManager.IsSignedIn(User) && User.IsInRole(Roles.Administrator))
{
    <div id="editArticle">

而不是像我在控制器中那样去检查角色,而不是像我在控制器中那样去检查角色.

but instead of checking after the role I'm after a check into the policy much like you would in a controller by doing this.

[Authorize(Policy = Policies.RequireAdmin)]

推荐答案

这似乎与

我发现此链接可能会有所帮助: https://docs .asp.net/en/latest/security/authorization/views.html

该页面上的示例:

@if (await AuthorizationService.AuthorizeAsync(User, "PolicyName"))
{
    <p>This paragraph is displayed because you fulfilled PolicyName.</p>
}

在某些情况下,资源将是您的视图模型,您可以调用 AuthorizeAsync的检查方式与您在检查期间完全相同 基于资源的授权;

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;

@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>
}

这篇关于在Asp.Net Core的Razor页面中检查登录用户AuthorizePolicy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆