azure - headless 身份验证 Azure AD b2c

标签 azure azure-ad-graph-api azure-ad-b2c

我正在寻找一种方法,以 Azure AD b2c 的 headless 方式通过用户名/密码对用户进行身份验证。 Azure AD b2c 很棒,但我们认为登录重定向可能会导致客户混淆(有时甚至被某些浏览器阻止)。此外,我们希望完全控制客户的用户体验体验。

我研究了 ADAL 和 Graph API,但还没有发现任何东西。

吉娜

最佳答案

如上所述here ,您可以将 Azure AD 应用程序用于 Client Credential Flow对于服务帐户。这不是最佳的,但它可以工作。

  1. Define an Azure AD App对于 Web API
  2. Define an Azure AD App每个服务帐户
  3. 配置 Web API 以接受来自 B2C 租户和 Azure AD 的 token
  4. 针对 Web API 的服务帐户 AD 应用请求访问 token

注意:请务必在 B2C 租户下创建 Azure AD 应用。

<小时/>

代码片段从 C# 获取访问 token

using (var httpClient = new HttpClient())
{
    httpClient.BaseAddress = new Uri("https://login.microsoftonline.com");

    var content = new FormUrlEncodedContent(new[]
    {
          new KeyValuePair<string, string>("grant_type", "client_credentials")
        , new KeyValuePair<string, string>("client_id", "[service account app id e.g. 10d635e5-7615-472f-8200-a81d5c87c0ca")
        , new KeyValuePair<string, string>("client_secret", "[client secret defined in the service account e.g. 5L2ZJOBK8GI1wRSgGFooHcBkAOUOj65lQd9DgJxQOrw=]")
        , new KeyValuePair<string, string>("scope", "[App ID URI of the web api azure ad app]/.default e.g. https://my-b2c-tenant.onmicrosoft.com/my-azure-ad-ap/.default")
    });

    var requestResult = await httpClient.PostAsync("/[your b2c tenant].onmicrosoft.com/oauth2/v2.0/token", content);
    var contentResult = await requestResult.Content.ReadAsStringAsync();

    var json = JObject.Parse(contentResult);
    var accessToken = (string)json["access_token"];
}

应用程序 ID URI

app id uri screenshot

<小时/>

您可能需要定义一些自定义声明来保护 Web API。请参阅'Application Permissions' here .

  1. 修改 Web API Azure AD 应用上的应用程序 list

    {
        "appRoles": [{
                "allowedMemberTypes": [
                    "Application"
                ],
                "displayName": "Some display nane",
                "id": "[create a new guid]",
                "isEnabled": true,
                "description": "Allow the application to _____ as itself.",
                "value": "the-blah-role"
            }
        ]
    }
    
  2. 向服务帐户 Azure AD 应用授予定义的自定义应用程序权限

授予服务帐户的权限将返回到 roles 声明中:

{
  "roles": [
    "the-blah-role"
  ]
}
<小时/>

请点赞the user voice feedback item让这变得更容易😀

关于azure - headless 身份验证 Azure AD b2c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35072371/

相关文章:

node.js - 如何使用 HTTP 触发器解析 Azure Function App 中的多部分表单数据? ( Node JS)

powershell - Cosmos DB 的 Powershell 查询出现 404

azure - 如何通过Azure AD API快速递归获取多个Azure AD组的成员?

azure - 通过 Microsoft Graph 检索用户列表时不支持查询

azure - Microsoft Graph API 与 IMAP/POP3 - 哪个更适合阅读邮件?

azure-ad-b2c - 显示密码/密码切换器标签的 Azure B2C 本地化

azure-ad-b2c - 没有电子邮件验证步骤的 Azure AD B2C 密码重置策略

node.js - 我可以使用 Mac 部署到 Azure 云服务吗?

windows - 是否可以配置 Tomcat 7 SSL 以通过 SunMSCAPI 访问证书?

c# - 无法从均受 Azure AD B2C 保护的 MVC Web 应用程序访问 WebApi