简体   繁体   English

授权属性 - 我应该如何使用它?

[英]Authorize attribute - how should I use it?

I used [Authorize(Roles = "Admininstrators")] to prevent users from accessing the web address under this authorization, but what should I do if I want to authorize this permission after successful login?我使用了[Authorize(Roles = "Admininstrators")]来阻止用户在这个授权下访问web地址,但是登录成功后要授权这个权限怎么办? I don't see the method on the official website.我在官网上没有看到方法。 I don't know how to authorize admininstrators我不知道如何授权管理员

在此处输入图像描述

To use [Authorize(Role)] , you must add a role to the user as in the method EnsureRole() -> await userManager.AddToRoleAsync(user, role);要使用[Authorize(Role)] ,您必须向用户添加角色,如EnsureRole() -> await userManager.AddToRoleAsync(user, role);

https://docs.microsoft.com/en-us/aspnet/core/security/authorization/secure-data?view=aspnetcore-5.0 https://docs.microsoft.com/en-us/aspnet/core/security/authorization/secure-data?view=aspnetcore-5.0


If you want to use claims for authorization, then check out this article https://docs.microsoft.com/en-us/aspnet/core/security/authorization/roles?view=aspnetcore-5.0如果您想使用声明进行授权,请查看这篇文章https://docs.microsoft.com/en-us/aspnet/core/security/authorization/roles?view=aspnetcore-5.0

you need to use [Authorization(Policy)] instead of [Authorization(Role)] and specify the necessary policies in Startup.cs您需要使用[Authorization(Policy)]而不是[Authorization(Role)]并在 Startup.cs 中指定必要的策略

services.Add authorization(parameters =>
{
  options.Add a policy ("Required Administrator Role",
  policy => policy.Required role ("Administrator"));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自定义授权属性时如何使用“ Roles”参数 - How can I use the “Roles” parameter when Customising the Authorize Attribute 如何在ASP.NET MVC应用程序中使用Authorize属性? - how i can use Authorize attribute in ASP.NET MVC application? 如果放入外部程序集,我无法使用自定义授权属性 - I cannot use my Custom Authorize attribute if put in an external assembly 使用没有身份的[授权]属性? - Use [Authorize] Attribute Without Identity? 如何使用IdentityServer4访问令牌和[Authorize]属性 - How to use IdentityServer4 Access Token and [Authorize] attribute 如何为角色以及特定用户使用自定义授权属性? - How to use custom Authorize attribute for roles as well as a specific user? 如何告诉[Authorize]属性使用自定义消息处理程序的基本身份验证? - How to tell [Authorize] attribute to use Basic authentication with custom message handler? 如何在控制器和操作级别使用 Authorize 属性? - How to use the Authorize attribute both at the controller and action level? 如何在[Authorize(Roles =“”)]中使用变量 - How can I use a variable in [Authorize(Roles=“”)] 如何在没有Authorize属性的情况下调用连接/授权 - How to call connect/authorize without Authorize attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM