【问题标题】:Allow all domains when adding a user to Azure B2C using the Graph API使用 Graph API 将用户添加到 Azure B2C 时允许所有域
【发布时间】:2021-02-27 19:04:58
【问题描述】:

我正在尝试通过 Graph API (C#) 将电子邮件地址为 ...@gmail.com 的用户添加到我的 B2C 目录中。我得到这个作为回应:

userPrincipalName 属性的域部分无效。你 必须使用您组织中经过验证的域名之一。

此系统需要允许任何电子邮件域的用户登录。用户需要登录网站,不能访问 Azure 门户。

有没有办法在不手动添加每个域的情况下完成此操作?

通过 Graph API 添加用户的代码:

var confidentialClientApplication = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithTenantId(tenantId)
    .WithClientSecret(clientSecret)
    .Build();

var authProvider = new ClientCredentialProvider(confidentialClientApplication);
            
var graphClient = new GraphServiceClient(authProvider);

var user = new User
{
    AccountEnabled = true, 
    DisplayName = emailAddress,
    MailNickname = emailAddress.Split('@').FirstOrDefault(),
    UserPrincipalName = emailAddress,
    PasswordProfile = new PasswordProfile
    {
        ForceChangePasswordNextSignIn = true,
        Password = tempPassword
    }
};

【问题讨论】:

    标签: microsoft-graph-api azure-ad-b2c


    【解决方案1】:

    我必须添加以下软件包:

    <PackageReference Include="Microsoft.Graph" Version="4.0.0-preview.7" />
    <PackageReference Include="Microsoft.Graph.Auth" Version="1.0.0-preview.7" />
    

    然后:

           var confidentialClientApplication = ConfidentialClientApplicationBuilder
                .Create(Settings.ClientId)
                .WithTenantId(Settings.Tenant)
                .WithClientSecret(Settings.ClientSecret)
                .Build();
            
            var authProvider = new ClientCredentialProvider(confidentialClientApplication);
    
            var graphClient = new GraphServiceClient(authProvider);
    
            var user = new User
            {
                AccountEnabled = true,
                GivenName = "Name",
                Surname = "Surname",
                DisplayName = "Name Surname",
                PasswordProfile = new PasswordProfile
                {
                    ForceChangePasswordNextSignIn = false,
                    Password = "pass.123",
                },
                PasswordPolicies = "DisablePasswordExpiration",
                Identities = new List<ObjectIdentity>
                {
                    new ObjectIdentity()
                    {
                        SignInType = "emailAddress",
                        Issuer = Settings.Tenant,
                        IssuerAssignedId = "sample@sample.com"
                    }
                }
            };
    
            await graphClient.Users.Request().AddAsync(user);
    

    确保添加在 Azure 门户中创建用户的权限。

    【讨论】:

    • 请注意,Settings.Tenant 应该是您的 AAD B2C 完整租户名称 (xxx.onmicrosoft.com)。
    【解决方案2】:

    如果您尝试创建本地 B2C(不是 AAD)帐户,请尝试在您的请求中设置 identities 属性,而不是 upn。最后一个应该是自动生成的。此外,密码过期必须禁用,下次登录时强制更改密码也必须禁用。

    【讨论】:

    • 在删除 MailNickname 和 UPN 并替换为:Identities = new List&lt;ObjectIdentity&gt; { new ObjectIdentity { SignInType = "emailAddress", Issuer = domainName, IssuerAssignedId = emailAddress} }, 后工作
    猜你喜欢
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    相关资源
    最近更新 更多