【问题标题】:Get access token on Microsoft federated accounts获取 Microsoft 联合帐户的访问令牌
【发布时间】:2018-11-14 13:23:06
【问题描述】:

我正在尝试获取 Power BI API 的访问令牌。我们的帐户是联合帐户。

我一直在尝试这个,但它一直给我一个错误,说用户名或密码不正确。为了使用资源所有者密码凭据授予流程来获取 Azure AD 的访问令牌,我使用 HttpClient 直接调用 http 请求

HttpClient clie = new HttpClient();
string tokenEndpoint = "https://login.microsoftonline.com/{tenant}/oauth2/token";
var body = "resource=https://analysis.windows.net/powerbi/api&client_id={client_id}&grant_type=password&username={username}&password={password}";
var stringContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded");
string result = clie.PostAsync(tokenEndpoint, stringContent).ContinueWith((response) =>
            {
                return response.Result.Content.ReadAsStringAsync().Result;
            }).Result;

这适用于非联合帐户。如何为联合帐户实施相同的操作?

【问题讨论】:

    标签: rest authentication azure-active-directory asp.net-core-2.0 powerbi


    【解决方案1】:

    利用 MSAL.NET(或 ADAL.NET)更容易实现这一目标。见https://aka.ms/msal-net-up

    scopes = new string[]{ "https://analysis.windows.net/powerbi/api/Dashboard.Read.All"}
    result = await app.AcquireTokenByUsernamePasswordAsync(scopes, "joe@contoso.com",
                                                           securePassword);
    

    如果您知道您的计算机已加入域或 AAD,则更好的是,您可以使用集成 Windows 身份验证:https://aka.ms/msal-net-iwa

    result = await app.AcquireTokenByIntegratedWindowsAuthAsync(scopes);
    

    请注意,我建议使用 MSAL.NET(而不是 ADAM.NET),因为使用 MSAL/NET/Azure AD v2.0 端点,PowerBI 可以更好地控制权限范围:

    https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredAppsPreview 中查看应用注册中的 API 权限选项卡

    【讨论】:

    • 第一个选项不适用于联合帐户。如果可以使用其他不涉及用户密码的流程就更好了。
    • 谢谢。该解决方案有效,尽管我使用了第一个。我将实施第二个,因为不推荐第一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 2017-10-07
    • 2012-05-27
    • 2021-12-02
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多