简体   繁体   English

如何在 asp net core mvc 应用程序中从我的 Azure AD B2C 获取用户列表?

[英]How to get list of Users from my Azure AD B2C in asp net core mvc app?

How to get list of Users from my Azure AD B2C in asp net core mvc application?如何在 asp net core mvc 应用程序中从我的 Azure AD B2C 获取用户列表?

You can use Azure Graph API to fetch all your users.您可以使用 Azure Graph API 来获取所有用户。 try the code below in .net core console app:在 .net 核心控制台应用程序中尝试以下代码:

using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {

            var tenantID = "<your tenant ID>";
            var clinetID = "<your app id>";
            var client_secret = "<your app password>";

            HttpClient client = new HttpClient();
            
            //get access token from Azure AD 
            var reqContent = @"grant_type=client_credentials&resource=https://graph.microsoft.com&client_id="+ clinetID + "&client_secret="+ System.Web.HttpUtility.UrlEncode(client_secret);
            var Content = new StringContent(reqContent, Encoding.UTF8, "application/x-www-form-urlencoded");
            var response = client.PostAsync("https://login.microsoftonline.com/"+ tenantID + "/oauth2/token", Content).Result;
            var token = JsonConvert.DeserializeObject<TokenResult>(response.Content.ReadAsStringAsync().Result);
           
            //Use access token to call microsoft graph api 
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token.access_token);
            Console.WriteLine(client.GetAsync("https://graph.microsoft.com/v1.0/users").Result.Content.ReadAsStringAsync().Result); 
            
            Console.ReadKey();

        }
    }

    class TokenResult
    {
        public string token_type { get; set; }
        public string expires_in { get; set; }
        public string ext_expires_in { get; set; }
        public string expires_on { get; set; }
        public string not_before { get; set; }
        public string resource { get; set; }
        public string access_token { get; set; }
    }

}

To run this code, you should register an app in your B2C tenant and grant read user permissions to it: Azure Active Directory => App registrations (Legacy) => New application registration :要运行此代码,您应该在 B2C 租户中注册一个应用程序并授予读取用户权限: Azure Active Directory =>应用程序注册(旧版) =>新应用程序注册

在此处输入图像描述

Note app id and create a password for your app and note it:记下应用 ID 并为您的应用创建密码并记下: 在此处输入图像描述

replace the value of clinetID with app id and replace the value of client_secret with password here.此处将clinetID的值替换为 app id,并将client_secret的值替换为密码。

grant read users permission to your app:授予读取用户对您的应用程序的权限: 在此处输入图像描述

CLICK "Grant permissions" BUTTON AFTER YOU SELECT PERMISSIONS FOR YOUR APP.在您的应用程序的 SELECT 权限之后单击“授予权限”按钮。

If you have any further concerns,pls feel free to let me know.如果您有任何进一步的疑虑,请随时告诉我。

Please reference the Azure Graph API.请参考 Azure 图表 API。

From the document:从文件:

The Azure Active Directory Graph API provides programmatic access to Azure AD through REST API endpoints. The Azure Active Directory Graph API provides programmatic access to Azure AD through REST API endpoints. Applications can use Azure AD Graph API to perform create, read, update, and delete (CRUD) operations on directory data and objects.应用程序可以使用 Azure AD Graph API 对目录数据和对象执行创建、读取、更新和删除 (CRUD) 操作。 For example, Azure AD Graph API supports the following common operations for a user object:例如,Azure AD Graph API 支持用户 object 的以下常用操作:

  • Create a new user in a directory在目录中创建一个新用户
  • Get a user's detailed properties, such as their groups获取用户的详细属性,例如他们的组
  • Update a user's properties, such as their location and phone number, or change their password更新用户的属性,例如他们的位置和电话号码,或更改他们的密码
  • Check a user's group membership for role-based access检查用户的组成员资格以获得基于角色的访问
  • Disable a user's account or delete it entirely禁用用户的帐户或完全删除它

https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-graph-api https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-graph-api

And here is a demo project which shows you how to list all users in your Azure B2C directory:这是一个演示项目,它向您展示如何列出 Azure B2C 目录中的所有用户:

https://github.com/AzureADQuickStarts/B2C-GraphAPI-DotNet/blob/master/B2CGraphClient/B2CGraphClient.cs#L43-L110 https://github.com/AzureADQuickStarts/B2C-GraphAPI-DotNet/blob/master/B2CGraphClient/B2CGraphClient.cs#L43-L110

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

相关问题 Azure B2C AD / dotnetcore 2.0 MVC 应用程序未授权 - Azure B2C AD / dotnetcore 2.0 MVC app not authorising Azure B2C保持在MVC 6 .Net Core 2中的签名 - Azure B2C Keep signed in MVC 6 .Net Core 2 ASP.NET Core MVC 6 Azure B2C Active Directory身份验证问题 - ASP.NET Core MVC 6 Azure B2C Active Directory Authentication issue 使用Azure AD身份验证(B2C)显示Core MVC的头像 - Showing avatar for Core MVC using Azure AD Authentication (B2C) 在针对 Azure AD 注销 ASP.NET Core MVC 应用程序时如何修复错误 404? - How to fix error 404 when logging out on an ASP.NET Core MVC app against Azure AD? 如何在ASP Net Core 2.0 MVC Web应用程序和Azure AD之间强制重新身份验证 - How to force re authentication between ASP Net Core 2.0 MVC web app and Azure AD Azure 应用服务上的 ASP.NET Core MVC Azure AD 身份验证循环 - ASP.NET Core MVC Azure AD Authentication Loop on Azure App Service Azure Web应用程序计划中的Azure AD B2C返回URL - Azure AD B2C return url in azure web app plan 有没有办法使用 Azure AD 对 ASP.NET Core MVC web 应用程序中的用户进行身份验证,但使用您自己的登录页面,而不是使用 Microsoft 的登录门户 - Is there a way to authenticate users in an ASP.NET Core MVC web app using Azure AD, but with a your own login page, not using Microsoft’s login portal Azure中的ASP.NET MVC Core应用 - ASP.NET MVC Core app in Azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM