根据之前关于我的帐户的问题,我和我的团队目前正在使用 C# 创建一个 Windows 窗体应用程序,以允许我们的管理员将公司及其所有用户注册到我们的系统中。目前,我们可以将单个用户注册到租户,并为他们提供公司 ID 的 GUID,他们应该分开,但这目前并没有授予他们访问权限。
在 B2C 中创建后,需要将它们推送到 Azure Cosmos DB 中的表中,以允许它们登录系统。
这是当前将用户添加到租户的方式:
private async Task<string> EnrollNewUser()
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "...";
var clientId = "...";
var clientSecret = "...";
ClientSecretCredential clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
string displayName = ESUDisplayNametxt.Text;
string compId = esuCompIdtxt.Text;
string givenName = ESUFirstNametxt.Text;
string surname = ESUSurnametxt.Text;
string phone = ESUPhoneNumtxt.Text;
string mail = ESUEmailtxt.Text;
string membershipLevel = ESUComboMembership.SelectedItem.ToString();
string groupId;
switch (membershipLevel)
{
case "No Membership":
groupId = "";
break;
case "Bronze":
groupId = "7118bcda-eb9d-433a-b37e-58c193c90b66";
break;
case "Silver":
groupId = "ae1044e3-b711-4d97-b73a-96de89c8e005";
break;
case "Gold":
groupId = "952894ab-7b7a-4f70-8b1d-ee7a8dbbd6aa";
break;
case "Admin":
groupId = "048fd54e-760e-41af-8e8f-c18f02f0d58d";
break;
default:
MessageBox.Show("Invalid option. Group ID left empty.");
groupId = "";
break;
}
string password = GeneratePassword();
List<string> businessPhones = new List<string> { phone };
var requestBody = new User
{
DisplayName = displayName,
City = compId,
GivenName = givenName,
Surname = surname,
BusinessPhones = businessPhones,
Mail = mail,
Identities = new List<ObjectIdentity>
{
new ObjectIdentity
{
SignInType = "emailAddress",
Issuer = "devB2CTennant.onmicrosoft.com",
IssuerAssignedId = mail,
}
},
PasswordProfile = new PasswordProfile
{
Password = password,
ForceChangePasswordNextSignIn = false,
},
PasswordPolicies = "DisablePasswordExpiration",
};
try
{
var user = await graphClient.Users.PostAsync(requestBody);
if (!string.IsNullOrEmpty(groupId))
{
var refRequestBody = new ReferenceCreate
{
OdataId = $"https://graph.microsoft.com/v1.0/directoryObjects/{user.Id}",
};
await graphClient.Groups[groupId].Members.Ref.PostAsync(refRequestBody);
}
MessageBox.Show($"User {displayName} created successfully with password: {password}");
}
catch (Exception ex)
{
MessageBox.Show($"Error creating user: {ex.Message}");
}
return password;
}
从这个角度来看这是否可能,或者是否需要更多逻辑来使 Cosmos 和 B2C 相互交互?
注意:Azure AD B2C 本身不支持将用户数据存储在 Azure Cosmos DB 等外部数据源中,因此此实现是自定义解决方案。
利用 API 连接器:
参考资料:
将 Azure AD B2C 用户映射到 Azure Sql 数据库表 - Microsoft 问答,作者:Alfredo Revilla
azure - 如何结合 AD B2C(MSAL) 和 CosmosDB - Stack Overflow 作者:Jas Suri - MSFT