【发布时间】:2019-08-22 15:55:34
【问题描述】:
我正在尝试为 Azure AD B2C 中的用户添加/更新自定义扩展属性的值。该属性是一个AccountNumber,类型为字符串。我正在使用两种不同的 Azure 环境。一个用于我的本地/暂存环境,另一个用于客户的生产 Azure 环境。我的本地和登台工作正常,但我似乎无法通过生产实例更新此属性,这导致我认为我在 Azure 实例本身而不是代码中缺少某种权限/配置,但是让我们看看。
以下是我在 Azure 中采取的步骤:
- 在 B2C 中,我设置了我的应用程序。对于 API 访问部分,我有 2 个选择
- 为用户获取一个 id_token (openid)
- 为用户获取一个 refresh_token (offline_access)
- 我有两个用户流,它们都将 AccountNumber 作为声明返回。
-
在应用注册(非旧版)中,我也添加了我的应用。它确实有关于在 B2C 中尚不支持的警告,但我在我的暂存实例中也有这个。对于已为此应用程序选择的 API 权限,我有以下内容。我来回添加了offline_access、openid 和profile。 (所有都是 uder Microsoft Graph Delegated)
- User.Read
- User.ReadWrite
- offline_access
- openid
- 个人资料
在应用注册/身份验证选项卡中,我启用了隐式授权流并检查了访问令牌和 ID 令牌
- 我已获取 b2c-extensions-app 应用 ID 并将其保存在我的代码中,用于更新扩展属性(已删除破折号)
Web.config
<add key="ida:NonAdminScopes" value="User.Read User.ReadWrite" />
<add key="ida:AdminScopes" value="Directory.AccessAsUser.All User.ReadWrite.All Group.ReadWrite.All" />
这是我正在构建和提出请求的地方。是的,我意识到我正在以更手动的方式执行此操作,但我也在与 Sitecore 合作,这要求我保留一些较旧的 dll,这就是我在经历了几天的挫折之后到达的地方。
private async Task<string> SendGraphPatchRequest(string json, string objectId)
{
var graphEndpoint = new Uri(string.Format("https://graph.microsoft.com/v1.0/{0}/users/{1}", Tenant, objectId));
HttpResponseMessage response;
using (var httpClient = new HttpClient { BaseAddress = graphEndpoint })
{
using (var requestMessage = new HttpRequestMessage(new HttpMethod("PATCH"), graphEndpoint))
{
var token = await GetOrCreateAccessToken();
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
if (!string.IsNullOrEmpty(json))
{
requestMessage.Content = new StringContent(json, Encoding.UTF8, "application/json");
}
response = await httpClient.SendAsync(requestMessage).ConfigureAwait(continueOnCapturedContext: false);
if (!response.IsSuccessStatusCode)
{
string error = await response.Content.ReadAsStringAsync();
Logger.Error(string.Format("Error -> RequestMessage: {0}", error));
object formatted = JsonConvert.DeserializeObject(error);
throw new WebException("Error Calling the Graph API: \n" + JsonConvert.SerializeObject(formatted, Formatting.Indented));
}
}
}
return response.Content.ReadAsStringAsync().Result;
}
创建访问令牌的行确实使用了 ClientCredentials
_accessToken = await _authContext.AcquireTokenAsync("https://graph.microsoft.com/", _credentials);
请求正文内容示例
{"extension_[extensionAppId]_AccountNumber":"123456"}
当我尝试使用 Postman 发出此请求时(我有一行代码记录了从上述代码 sn-p 创建的令牌 - 不确定这是否真的有效),这里是我得到的回复:
{
"error": {
"code": "Authorization_RequestDenied",
"message": "Insufficient privileges to complete the operation.",
"innerError": {
"request-id": "####",
"date": "2019-08-21T15:06:45"
}
}
}
【问题讨论】:
-
您确定在生产环境中使用了正确的 b2c-extensions-app ID 吗?这是在黑暗中拍摄的,但您也可以尝试docs.microsoft.com/en-us/azure/active-directory-b2c/…中的第 1.4 步
标签: c# azure-ad-b2c