ASP Core Azure Active Directory 登录使用角色

ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登录使用角色)
本文介绍了ASP Core Azure Active Directory 登录使用角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我创建了一个 Azure Active Directory 应用程序,并且我想使用基于角色的安全性.我按照以下教程进行操作:

I created an Azure Active Directory Application and i want to use role based security. I followed the tutorial on: https://azure.microsoft.com/en-us/resources/samples/active-directory-dotnet-webapp-openidconnect-aspnetcore/

The login works, I added roles to the application manifest and assigned the role Approver to my own account. Now i want to use these roles.

After login the following works in the controller:

[Authorize]

But when adding the role the user is not authorized:

[Authorize(Roles="Approver")]

Also the following returns false:

User.IsInRole("Approver");

It seems the roles are not retreived, any suggestions on how to add the role functionality to this demo project?

解决方案

This code sample works for me after assign roles to account . Please debug application in this line: User.IsInRole("Approver"); , check whether {http://schemas.microsoft.com/ws/2008/06/identity/claims/role: Approver}exists in user claims . And make sure you add roles which allowedMemberTypes is user , for example :

{
      "allowedMemberTypes": [
        "User"
      ],
      "displayName": "Approver",
      "id": "fc803414-3c61-4ebc-a5e5-cd1675c14bbb",
      "isEnabled": true,
      "description": "Approvers have the ability to change the status of tasks.",
      "value": "Approver"
    },

And you have assign the user role in Enterprise applications-->All applications--> find your app-->Users and groups--> add/edit a user and assign roles :

这篇关于ASP Core Azure Active Directory 登录使用角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to mock Controller.User using moq(如何使用 moq 模拟 Controller.User)
The application named HTTPS://test113.onmicrosoft.com/FTP was not found in the tenant named test113.onmicrosoft.com(在名为 test113.onmicrosoft.com 的租户中找不到名为 HTTPS://test113.onmicrosoft.com/FTP 的应用程序) - IT屋-程序员软件开发技术
Using MSAL and Angular why would one need two separate AAD app registrations?(使用 MSAL 和 Angular 为什么需要两个单独的 AAD 应用注册?)
How to configure .net core angular azure AD authentication?(如何配置.net core angular azure AD 身份验证?)
How do I delete an AppRoleAssignment using the Azure Active Directory .NET SDK?(如何使用 Azure Active Directory .NET SDK 删除 AppRoleAssignment?)
Role Count using Graph Api against a tenant(使用 Graph Api 对租户进行角色计数)