代码之家  ›  专栏  ›  技术社区  ›  Newton Sheikh

获取Azure访问令牌

  •  3
  • Newton Sheikh  · 技术社区  · 8 年前

    我犯了这个严重的错误,我不知道我在这里做错了什么。我的代码甚至看起来与这个博客上的代码非常相似,但在我的情况下,这不起作用。

    GetAzureAccessToken

    错误消息

    “'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;
        }
    
    1 回复  |  直到 8 年前
        1
  •  5
  •   juunas    8 年前

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

    var authCtx = new AuthenticationContext(loginUri + tenantId);
    

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