【问题标题】:How to acquire a token with Azure AD and MSAL in ASP.NET如何在 ASP.NET 中使用 Azure AD 和 MSAL 获取令牌
【发布时间】:2021-03-19 04:55:36
【问题描述】:

我正在尝试使用 Azure AD 对令牌进行身份验证。在控制台应用程序中,感谢 IConfidentialClientApplication,我对此没有任何问题:

static void Main(string[] args)
{
    Console.WriteLine("Making the call...");
    RunAsync().GetAwaiter().GetResult();
}

private static async Task RunAsync()
{
    AuthConfig config = AuthConfig.ReadJsonFromFile("appsettings.json");

    IConfidentialClientApplication app;

    app = ConfidentialClientApplicationBuilder.Create(config.ClientId)
        .WithClientSecret(config.ClientSecret)
        .WithAuthority(new Uri(config.Authority))
        .Build();

    string[] ResourceIds = new string[] { config.ResourceId };

    AuthenticationResult result = null;

    try
    {
        result = await app.AcquireTokenForClient(ResourceIds).ExecuteAsync();
    }
    ...
}

但在 ASP.NET Core 应用程序的启动中,该应用程序使用标准 IApplicationBuilder,并且 Configure(...) 方法不能采用 IConfidentialClientApplicationBuilder,因为它不存在。

Microsoft's documentation 中,他们创建了一个PublicClientApplicationBuilder,但我不想开始在我的配置中创建全新的应用程序。

【问题讨论】:

    标签: azure asp.net-core .net-core azure-active-directory msal


    【解决方案1】:

    这里有一个sample 的 Startup for ASP.NET Core 应用程序供您参考。但据我所知,我们无法使用“IApplicationBuilder”直接在 ASP.NET Core 应用程序的启动中获取令牌。 “IApplicationBuilder”和“IConfidentialClientApplicationBuilder”是两个不同的概念。

    根据您的要求,您可以只为 msal 导入模块并使用您在上面提供的代码或您在Microsoft's documentation 中提到的代码来获取令牌。

    【讨论】:

      猜你喜欢
      • 2019-06-19
      • 1970-01-01
      • 2020-10-17
      • 2023-04-04
      • 2020-03-10
      • 2021-12-28
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      相关资源
      最近更新 更多