【Azure Developer】在微软云中国区,如何使用Microsoft GraphAPI连接到B2C Tenant

问题描述

如题所述,当在中国区使用Microsoft GraphAPI连接B2C Tenant时候,如何来设置中国区的Endpoint呢?在GitHub的示例中,并没有示例介绍如何连接中国区。如

 

问题解决

由于中国区的Azure是独立的运行环境,所以与Global不一样。在终结点方面表现最为明显,如在使用China Azure的开发中,需要特别注意终结点部分:

结合示例代码,修改成指向中国区的代码为:

复制代码
            AppSettings config = AppSettingsFile.ReadFromJsonFile();

            // Initialize the client credential auth provider
            IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
                .Create(config.AppId)
                .WithTenantId(config.TenantId)
                .WithClientSecret(config.ClientSecret)
                .Build();
            ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication, "https://microsoftgraph.chinacloudapi.cn/.default");
            
            // Set up the Microsoft Graph service client with client credentials
            var httpClient = GraphClientFactory.Create(authProvider, nationalCloud: GraphClientFactory.China_Cloud);
            GraphServiceClient graphClient = new GraphServiceClient(httpClient);
            graphClient.BaseUrl = "https://microsoftgraph.chinacloudapi.cn/v1.0";
复制代码

 

注意: 如果在运行代码时,遇见权限不足的问题,需要先确认AAD中所注册的应用(Registrations Application)是否有Microsoft Graph的相关权限.

 

参考资料

中国区 Azure 开发人员入门指南https://docs.azure.cn/zh-cn/guides/developer/azure-developer-guide

Microsoft Graph .NET Authentication Provider Libraryhttps://github.com/microsoftgraph/msgraph-sdk-dotnet-auth

posted @   路边两盏灯  阅读(266)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示