You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Azure AD Graph迁移至Microsoft Graph:获取API参数的替代方案

替代已弃用的Microsoft.WindowsAzure.ConfigurationManager的方案

针对你的.NET应用迁移到Microsoft Graph API的场景,以下是几种主流的配置参数获取替代方案,适配不同.NET版本:

1. Microsoft.Extensions.Configuration(推荐,适用于.NET Core/.NET 5+)

这是.NET官方目前主推的配置系统,支持多种配置源(JSON、XML、环境变量、命令行等),跨平台友好。

使用步骤:

  • 安装对应配置源的NuGet包,比如JSON配置:
    Install-Package Microsoft.Extensions.Configuration.Json
    
  • 构建配置实例并获取参数:
    var configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json") // 可替换为XML、环境变量等源
        .Build();
    
    // 获取原配置键对应的参数
    var graphApiUrl = configuration["ida_AADGraphApi"];
    var graphEndpoint = configuration["ida_AADGraphApiApplicationEndpoint"];
    
  • 如果是ASP.NET Core项目,可直接通过依赖注入使用IConfiguration:
    public class GraphApiService
    {
        private readonly IConfiguration _configuration;
    
        public GraphApiService(IConfiguration configuration)
        {
            _configuration = configuration;
        }
    
        public async Task CallGraphApi()
        {
            var graphApiUrl = _configuration["ida_AADGraphApi"];
            // 执行Microsoft Graph API调用逻辑
        }
    }
    

2. System.Configuration.ConfigurationManager(适用于.NET Framework 4.x)

如果你仍在维护传统.NET Framework项目,直接使用框架原生的ConfigurationManager即可,无需额外安装包(需引用System.Configuration程序集)。

代码示例:

using System.Configuration;

// 直接读取AppSettings中的配置
var graphApiUrl = ConfigurationManager.AppSettings["ida_AADGraphApi"];
var graphEndpoint = ConfigurationManager.AppSettings["ida_AADGraphApiApplicationEndpoint"];

3. Azure Key Vault集成(生产环境敏感参数推荐)

如果你的配置参数(如客户端密钥、租户ID)属于敏感信息,建议存储在Azure Key Vault中,通过配置系统加载:

使用步骤:

  • 安装NuGet包:
    Install-Package Azure.Extensions.AspNetCore.Configuration.Secrets
    
  • 构建配置并加载Key Vault中的参数:
    var configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json")
        .AddAzureKeyVault(new Uri("https://your-key-vault-name.vault.azure.net/"), new DefaultAzureCredential())
        .Build();
    
    // 直接获取Key Vault中存储的参数(键名需与Vault中的秘密名称一致)
    var graphApiUrl = configuration["ida_AADGraphApi"];
    

新手迁移建议

  • 优先适配.NET最新配置系统:如果你的应用正在升级到.NET 5+,建议直接采用Microsoft.Extensions.Configuration,后续维护和扩展更方便。
  • 保留原有配置键名:无需修改配置文件中的键名,仅替换参数获取代码,减少迁移工作量。
  • 敏感参数隔离:避免将客户端ID、密钥等敏感信息放在明文配置文件中,使用Azure Key Vault或环境变量存储更安全。
  • 分步验证:先单独测试配置参数是否能正确加载,再集成Microsoft Graph API的调用逻辑,排查问题更高效。

内容的提问来源于stack exchange,提问作者Kotana Sai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 04:21:19