我正在使用 MS Graph API 在 AD B2C 上注册用户,当我尝试不添加其他用户属性时,它可以工作,但它失败并出现错误
属性不可用
当我尝试添加那些用户自定义属性时。
我确实遵循了其他解决方案线程,并clientid
在字典中设置属性名称时添加了不带“-”的符号,但没有起作用。
这是我的代码片段:
public class UserService : IUserService
{
private readonly ILogger<UserService> logger;
private readonly GraphServiceClient graphServiceClient;
private readonly string[] supportedRegionCodes;
private readonly string _clientId;
public UserService(ILogger<UserService> logger, IOptions<B2CConfiguration> options)
{
this.logger = logger;
ClientSecretCredentialOptions credentialOptions = new()
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
ClientSecretCredential clientSecretCredential = new(
options.Value.TenantId,
options.Value.ClientId,
options.Value.ClientSecret,
credentialOptions);
_clientId = options.Value.ClientId;
this.graphServiceClient = new GraphServiceClient(clientSecretCredential, options.Value.Scopes);
this.supportedRegionCodes = options.Value.SupportedRegionCodes;
}
public async Task<string?> RegisterUserToB2C(object userDetails)
{
try
{
string clientId = _clientId.Replace("-", string.Empty);
var user = new User
{
Identities = new List<ObjectIdentity>
{
new ObjectIdentity
{
SignInType = "userName",
Issuer ="empxxxqa.onmicrosoft.com",
IssuerAssignedId = "testuser011"
}
},
DisplayName = "Test User",
GivenName = "Test",
Surname = "User",
Mail = "[email protected]",
OtherMails = new List<string> { "[email protected]" },
PasswordProfile = new PasswordProfile
{
Password = "@w0rd123!",
ForceChangePasswordNextSignIn = true
},
UserType = "Member",
AdditionalData = new Dictionary<string, object>
{
{$"extension_{clientId}_CardId","TEST"}
}
};
var created = await graphServiceClient.Users.PostAsync(user);
return created?.Id;
}
catch (Exception ex)
{
return ex.Message;
}
}
}
这是我收到的错误:
以下扩展属性不可用:extension_3a84xxxxxxxxxxx126f_CardId。
我还添加了我们在 b2c 中创建的属性的屏幕截图:
我对 AD B2C 和 MS Graph API 比较陌生,需要在 B2C 层面添加什么吗?由于没有额外的自定义用户属性,我尝试注册时成功了,所以我认为应该没有权限问题导致注册失败。