使用 Microsoft Graph SDK 访问国家云部署

默认情况下,Microsoft Graph SDK 配置为访问 Microsoft Graph 全局服务中的数据,使用 https://graph.microsoft.com 根 URL 访问 Microsoft Graph REST API。 开发人员可以重写此配置以连接到 Microsoft Graph 国家/地区云部署

先决条件

需要以下信息才能配置 Microsoft Graph SDK 以连接到国家云部署。

配置 SDK

若要连接到国家云部署,必须将 身份验证提供程序 配置为连接到正确的令牌服务终结点。 然后,必须将 SDK 客户端配置为连接到正确的 Microsoft Graph 服务根终结点。

权限范围

任何权限范围值 (包括 .default 包含 Microsoft Graph 域的范围) 都必须使用 Microsoft Graph 服务根终结点的域进行国家云部署。 缩短的权限范围名称(如 User.ReadMail.Send)也有效。

  • 对于 增量或动态同意User.Readhttps://graph.microsoft.us/User.Read 等效于美国政府 L4 国家/地区云。
  • 对于 静态定义的权限,或者如果使用 客户端凭据流 获取仅限应用的权限, https://graph.microsoft.us/.default 则 为正确的 .default 范围值。

示例

以下示例使用 Microsoft Graph SDK 配置 交互式身份验证提供程序 ,以连接到适用于美国政府 L4 国家/地区云的 Microsoft Graph。

// Create the InteractiveBrowserCredential using details
// from app registered in the Azure AD for US Government portal
var credential = new InteractiveBrowserCredential(
    "YOUR_TENANT_ID",
    "YOUR_CLIENT_ID",
    new InteractiveBrowserCredentialOptions
    {
        // https://login.microsoftonline.us
        AuthorityHost = AzureAuthorityHosts.AzureGovernment,
        RedirectUri = new Uri("YOUR_REDIRECT_URI"),
    });

// Create the authentication provider
var authProvider = new AzureIdentityAuthenticationProvider(
    credential,
    ["https://graph.microsoft.us/.default"]);

// Create the Microsoft Graph client object using
// the Microsoft Graph for US Government L4 endpoint
// NOTE: The API version must be included in the URL
var graphClient = new GraphServiceClient(
    authProvider,
    "https://graph.microsoft.us/v1.0");