【问题标题】:Azure AD Identity.Client Calling AcquireTokenByUsernamePassword, get 406 errorAzure AD Identity.Client 调用 AcquireTokenByUsernamePassword,得到 406 错误
【发布时间】:2019-07-09 13:40:35
【问题描述】:

更新:我回到 Azure AD 并创建了一个新用户。仔细复制新的用户名和密码,代码有效。我之前使用了创建目录并抛出 406 的用户的用户名(电子邮件)和密码,不知道为什么?

我下载了 Azure 上控制台代码。我按照自述文件如下

当控制台出现时,这是我输入的内容 用户名:用于登录 Azure 门户的电子邮件 密码:用于登录 Azure Portal 的密码

在示例代码中,调用了以下代码

private async Task<AuthenticationResult> GetTokenForWebApiUsingUsernamePasswordAsync(IEnumerable<string> scopes, string username, SecureString password)
        {
            AuthenticationResult result = null;

                result = await App.AcquireTokenByUsernamePassword(scopes, username, password)
                    .ExecuteAsync();

            return result;
}

在json文件中,我有以下设置

{
  "Authentication": {
    // Azure Cloud instance among:
    // - AzurePublic (see https://aka.ms/aaddevv2). This is the default value
    // - AzureUsGovernment (see https://docs.microsoft.com/azure/azure-government/documentation-government-developer-guide)
    // - AzureChina (see https://docs.microsoft.com/azure/china/china-get-started-developer-guide)
    // - AzureGermany (See https://docs.microsoft.com/azure/germany/germany-developer-guide)
    "AzureCloudInstance": "AzurePublic",

    // Azure AD Audience among:
    // - AzureAdMyOrg (single tenant: you need to also provide the TenantId
    // - AzureAdMultipleOrgs (multi-tenant): Any work and school accounts
    // - AzureAdAndPersonalMicrosoftAccount (any work and school account or Microsoft personal account)
    // - PersonalMicrosoftAccount (Microsoft personal account only)
    "AadAuthorityAudience": "AzureAdMyOrg",


    "ClientId": "copied from azure ad client id",

    "TenantId": "copied from azure ad tenant id",

    "Tenant": "copied from azure ad tenant id"
  },          
注册客户端应用程序(上控制台)
  1. 应用注册(预览版)页面中,选择新注册
  2. 注册应用程序页面出现时,输入您的应用程序的注册信息:
    • 名称部分,输入一个有意义的应用名称,该名称将显示给应用的用户,例如up-console
    • 支持的帐户类型部分,选择任何组织目录中的帐户

      请注意,如果有多个重定向 URI,您需要稍后在成功创建应用后从 身份验证 选项卡中添加它们。

  3. 选择注册以创建应用程序。
  4. 在应用Overview页面,找到Application (client) ID值,记录下来以备后用。您需要它来配置此项目的 Visual Studio 配置文件。
  5. 在应用的页面列表中,选择Manifest,然后:
    • 在清单编辑器中,将allowPublicClient 属性设置为true
    • 在清单编辑器上方的栏中选择保存
  6. 在应用的页面列表中,选择API 权限

    • 点击添加权限按钮,然后,
    • 确保选中 Microsoft API 选项卡
    • 常用的 Microsoft API 部分,点击 Microsoft Graph
    • Delegated permissions 部分中,确保检查了正确的权限:User.ReadUser.ReadBasic.All。如有必要,请使用搜索框。
    • 选择添加权限按钮
  7. 在此阶段已正确分配权限,但客户端应用程序不允许交互。 因此,不能通过 UI 表示同意并接受使用服务应用程序。 点击Grant/revoke admin consent for {tenant} 按钮,然后在系统询问您是否要同意 请求租户中所有帐户的权限。 您需要成为 Azure AD 租户管理员才能执行此操作。

第 3 步:配置示例以使用您的 Azure AD 租户

在以下步骤中,“ClientID”与“Application ID”或“AppId”相同。

在 Visual Studio 中打开解决方案以配置项目

配置客户端项目

注意:如果您使用了设置脚本,以下更改将适用于您

  1. 打开up-console\appsettings.json文件
  2. 找到应用程序密钥 ClientId 并将现有值替换为从 Azure 门户复制的 up-console 应用程序的应用程序 ID (clientId)。
  3. (可选)找到设置Tenant 的行并将现有值替换为您的租户ID。

第 4 步:运行示例

清理解决方案,重新构建解决方案,然后在调试器中启动它。

【问题讨论】:

    标签: .net azure azure-active-directory


    【解决方案1】:

    我回到 Azure AD 并创建了一个新用户。仔细复制新的用户名和密码,代码有效。我之前使用了创建目录并抛出 406 的用户的用户名(电子邮件)和密码,不知道为什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 2020-10-09
      • 2023-04-08
      • 2021-12-23
      • 2018-01-01
      • 2015-12-10
      • 2016-07-20
      相关资源
      最近更新 更多