通过外部应用程序将用户添加到 Azure AD B2C - NullException

问题描述 投票:0回答:1

我一直在开发一个管理应用程序,该应用程序允许用户查看、删除新用户以及向 Azure AD B2C 租户添加新用户。问题是当它到达 .AddAsync(newUser); 时,我不断收到“System.NullReferenceException”。我已确保该应用程序在 azure 中具有 read.write 权限,并且tenantId、AppID 和客户端密钥均正确,我还更新了所有包和 .net

下面是 CreateUser 的片段,后面是“局部变量?”异常的图像,我注意到createdUser 的值为 null,但不确定这意味着什么。预先感谢

public static async Task CreateUser(GraphServiceClient graphClient)
{
    Console.WriteLine("Creating a new user...");

    // Collect user input for new user details
    Console.Write("Enter display name: ");
    string displayName = Console.ReadLine();

    Console.Write("Enter given name: ");
    string givenName = Console.ReadLine();

    Console.Write("Enter surname: ");
    string surname = Console.ReadLine();

    Console.Write("Enter email address: ");
    string emailAddress = Console.ReadLine();

    Console.Write("Enter password: ");
    string password = Console.ReadLine();


    var newUser = new UserModel
    {
        DisplayName = displayName,
        GivenName = givenName,
        Surname = surname,
        Password = password,
        Identities = new List<ObjectIdentity>
{
    new ObjectIdentity
    {
        SignInType = "emailAddress",
        IssuerAssignedId = emailAddress
    }
}
    };

    try
    {

        var createdUser = await graphClient.Users
            .Request()
            .AddAsync(newUser);

        Console.WriteLine($"User '{createdUser.DisplayName}' with ID '{createdUser.Id}' successfully created.");
    }
    catch (Exception ex)
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine($"Error creating user: {ex.Message}");
        Console.ResetColor();
    }
}

在此输入图片描述

我已确保应用程序在azure中具有读.写权限,并且tenantId、AppID和客户端密钥均正确,我还更新了所有包和.nets

这是尝试加入公司(添加用户)时控制台显示的内容 在此输入图片描述

c# azure-ad-b2c
1个回答
0
投票

我在本地环境中尝试了与控制台应用程序相同的操作,下面的代码工作正常。

using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;

namespace GraphApiConsole;

class Program
{
    static void Main(string[] args)
    {
        var scopes = new[] { "https://graph.microsoft.com/.default" };

        // Multi-tenant apps can use "common",
        // single-tenant apps must use the tenant ID from the Azure portal
        var tenantId = "<tenantId>";

        // Value from app registration
        var clientId = "<clientId>";
        var clientSecret = "<clientSecret>";

        ClientSecretCredential clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
        var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

        var users = graphClient.Users.GetAsync().Result;
        Console.WriteLine("Users:{0}", users.Value.Count);
        Console.WriteLine("Creating a new user...");

        // Collect user input for new user details
        Console.Write("Enter display name: ");
        string displayName = Console.ReadLine();

        Console.Write("Enter given name: ");
        string givenName = Console.ReadLine();

        Console.Write("Enter surname: ");
        string surname = Console.ReadLine();

        Console.Write("Enter email address: ");
        string emailAddress = Console.ReadLine();

        Console.Write("Enter password with Enough Complexity: ");
        string password = Console.ReadLine();

        var requestBody = new User
        {
            DisplayName = displayName,
            GivenName = givenName,
            Surname = surname,
            Identities = new List<ObjectIdentity>
            {
                new ObjectIdentity
                {
                   SignInType = "emailAddress",
                   Issuer = "carbooking.onmicrosoft.com",
                   IssuerAssignedId = emailAddress,
                }
            },
            PasswordProfile = new PasswordProfile
            {
                Password = password,
                ForceChangePasswordNextSignIn = false,
            },
            PasswordPolicies = "DisablePasswordExpiration",
        };

        try
        {
            var result = graphClient.Users.PostAsync(requestBody).Result;
            Console.WriteLine("User created successfully");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error creating user: {ex.Message}");
        }
    }
}

我仅使用 nuget 包的更新版本

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Azure.Identity" Version="1.11.2" />
    <PackageReference Include="Microsoft.Graph" Version="5.51.0" />
  </ItemGroup>

</Project>

代码输出在这里 enter image description here

用户是在adb2c中创建的

enter image description here

了解更多信息。 您的 AD b2c 应用程序必须拥有

User.ReadWrite.All
并且需要授予
Directory.ReadWrite.All
权限才能执行此操作。

  1. 密码一定要强
  2. 问题应该是您的租户 ID,例如
    ≤TENANTNAME≥.onmicrosoft.com

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.