我正在寻找一种方法,以 Azure AD b2c 的 headless 方式通过用户名/密码对用户进行身份验证。 Azure AD b2c 很棒,但我们认为登录重定向可能会导致客户混淆(有时甚至被某些浏览器阻止)。此外,我们希望完全控制客户的用户体验体验。
我研究了 ADAL 和 Graph API,但还没有发现任何东西。
吉娜
最佳答案
如上所述here ,您可以将 Azure AD 应用程序用于 Client Credential Flow对于服务帐户。这不是最佳的,但它可以工作。
- Define an Azure AD App对于 Web API
- Define an Azure AD App每个服务帐户
- 配置 Web API 以接受来自 B2C 租户和 Azure AD 的 token
- 假设您已经为 B2C 配置了 Web API...
- Azure AD 应用程序的众所周知的配置 URL 是 https://login.microsoftonline.com/[your-b2c-tenant].onmicrosoft.com/.well-known/openid-configuration
- 进一步阅读:ASP.NET Core 文档:Use multiple authentication schemes
- 针对 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
您可能需要定义一些自定义声明来保护 Web API。请参阅'Application Permissions' here .
修改 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" } ] }
向服务帐户 Azure AD 应用授予定义的自定义应用程序权限
授予服务帐户的权限将返回到 roles
声明中:
{
"roles": [
"the-blah-role"
]
}
<小时/>
请点赞the user voice feedback item让这变得更容易😀
关于azure - headless 身份验证 Azure AD b2c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35072371/