无头身份验证Azure AD B2C [英] headless authentication Azure AD b2c

查看:19
本文介绍了无头身份验证Azure AD B2C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我研究了Adal和Graph API,但尚未找到任何东西。

吉娜

推荐答案

here所述,您可以为Client Credential Flow服务帐户使用Azure AD App。它不是最优的,但它是有效的。

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

注意:确保在您的B2C租户下创建Azure AD应用。


代码段从C#获取访问令牌

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"];
}

App ID URI


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

  1. 修改Web API Azure AD App上的应用程序清单

    {
        "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 App权限授予定义的自定义应用程序权限

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

{
  "roles": [
    "the-blah-role"
  ]
}

请投赞成票the user voice feedback item以使😀更容易

这篇关于无头身份验证Azure AD B2C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆