如何在C#中获取MsalToken

问题描述 投票:0回答:1

我可以使用这些参数获得不记名令牌

Get-MsalToken -ClientId '' -TenantId '' -Scopes ''

我正在尝试将其转换为使用 C# 而不是 PowerShell:

var clientId = "xyz";
var tenantId = "xyz";

// We intend to obtain a token for Graph for the following scopes (permissions)
string[] scopes = { $"{clientId}/.default" };

string authority = $"https://login.microsoftonline.com/{tenantId}";

IPublicClientApplication app = PublicClientApplicationBuilder.Create(clientId)
    .WithAuthority(new Uri(authority))
    .WithRedirectUri("http://localhost")
    .Build();

AuthenticationResult result = await app.AcquireTokenInteractive(scopes).ExecuteAsync();
var token =  result.AccessToken;

但是我收到一条错误消息:“请求中指定的重定向 URI 'http://localhost' 与为应用程序配置的重定向 URI 不匹配”

azure azure-powershell azure-sdk-.net
1个回答
0
投票

发生这种情况是因为 Azure 中的应用程序未配置为重定向到

localhost
。您需要在 Azure 门户中更新它。请参阅:https://learn.microsoft.com/en-us/troubleshoot/azure/active-directory/error-code-aadsts50011-redirect-uri-mismatch

© www.soinside.com 2019 - 2024. All rights reserved.