获取 Azure 访问令牌

New*_*ikh 5 azure access-token azure-active-directory

我遇到了这个令人讨厌的错误,我不知道我在这里做错了什么。我的代码甚至看起来与此博客上的代码非常相似,但就我而言,这是行不通的。

获取AzureAccessToken

错误信息

“'authority' Uri 路径中应至少有一个段(即 https:////...)\r\n参数名称:authority”

public static string GetAzureAccessToken(string clientId, string clientSecret)
    {
        string tenandId = "{tenandId}";
        string loginUri = $"https://login.windows.net/";

        var authCtx =
            new AuthenticationContext(String.Format(CultureInfo.InvariantCulture, loginUri, tenandId));
        var credential = new ClientCredential(clientId: clientId, clientSecret: clientSecret);

        var result = authCtx.AcquireToken(resource: "https://management.core.windows.net/",
            clientCredential: credential);

        if(result == null)
            throw new InvalidOperationException("Failed to aquire token");

        return result.AccessToken;
    }
Run Code Online (Sandbox Code Playgroud)

juu*_*nas 8

你定义的权限错误,应该是:

var authCtx = new AuthenticationContext(loginUri + tenantId);
Run Code Online (Sandbox Code Playgroud)

另外,我想这并不重要,但我https://login.microsoftonline.com/tenant-id通常用作权威。