我拥有像
User.ReadWrite.All
、Directory.ReadWrite.All
这样的所有权限,并且此权限类型是 Application
。
我认为在这种情况下存在权限问题,我想使用此 c# 代码创建一个新用户。
当我尝试创建新用户时出现此错误:
the expression cannot be evaluated. A common cause of this error is attempting to pass a lambda into a delegate.
public async Task createNewUser()
{
try
{
var requestBody = new User
{
AccountEnabled = true,
City = "Surat",
Country = "India",
Department = "IT",
DisplayName = "Test User Unknown",
GivenName = "Unknown",
JobTitle = "SharePoint Developer",
MailNickname = "UnknownT",
PasswordPolicies = "DisablePasswordExpiration",
PasswordProfile = new PasswordProfile
{
Password = "0296db04-e2c3-cbec-993b-663e59e50f1c",
ForceChangePasswordNextSignIn = false,
},
OfficeLocation = "131/1105",
PostalCode = "395010",
PreferredLanguage = "en-US",
State = "Gujarat",
StreetAddress = "9256 Towne Center Dr., Suite 400",
Surname = "Test",
MobilePhone = "+91 1324567891",
UsageLocation = "India",
UserPrincipalName = "[email protected]",
};
var result = await GraphClient.Users.PostAsync(requestBody);
Console.WriteLine("User Created Successfully.");
}
catch (ServiceException ex)
{
Console.WriteLine($"Error getting user details: {ex.Message}");
}
}```
要创建用户,请确保授予
User.ReadWrite.All
应用程序 tpye API 权限:
要使用您提供的请求正文创建用户,请使用以下代码:
使用位置必须是印度,而不是印度
using System;
using System.Threading.Tasks;
using Microsoft.Graph;
using Azure.Identity;
using Microsoft.Graph.Models.ODataErrors;
using Microsoft.Graph.Models;
namespace UserProperties
{
public class GraphHandler
{
public GraphServiceClient GraphClient { get; set; }
public GraphHandler(string tenantId, string clientId, string clientSecret)
{
GraphClient = CreateGraphClient(tenantId, clientId, clientSecret);
}
public GraphServiceClient CreateGraphClient(string tenantId, string clientId, string clientSecret)
{
var options = new TokenCredentialOptions
{
AuthorityHost = Azure.Identity.AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
var scopes = new[] { "https://graph.microsoft.com/.default" };
return new GraphServiceClient(clientSecretCredential, scopes);
}
public async Task<bool> CreateUser(User user)
{
try
{
await GraphClient.Users.PostAsync(user);
Console.WriteLine("User created successfully.");
return true;
}
catch (ODataError odataError)
{
Console.WriteLine($"OData error details:");
Console.WriteLine($"Code: {odataError.Error?.Code}");
Console.WriteLine($"Message: {odataError.Error?.Message}");
throw;
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
return false;
}
}
}
class Program
{
static async Task Main(string[] args)
{
try
{
var tenantId = "TenantID";
var clientId = "ClientID";
var clientSecret = "ClientSecret";
var handler = new GraphHandler(tenantId, clientId, clientSecret);
var requestBody = new User
{
AccountEnabled = true,
City = "Surat",
Country = "India",
Department = "IT",
DisplayName = "Test User Unknown",
GivenName = "Unknown",
JobTitle = "SharePoint Developer",
MailNickname = "UnknownT",
PasswordPolicies = "DisablePasswordExpiration",
PasswordProfile = new PasswordProfile
{
Password = "***",
ForceChangePasswordNextSignIn = false,
},
OfficeLocation = "131/1105",
PostalCode = "395010",
PreferredLanguage = "en-US",
State = "Gujarat",
StreetAddress = "9256 Towne Center Dr., Suite 400",
Surname = "Test",
MobilePhone = "+91 1324567891",
UsageLocation = "IN",
UserPrincipalName = "[email protected]",
};
await handler.CreateUser(requestBody);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
}
用户已成功创建并具有所有属性:
我修改了代码以打印
ODataErrors
,这样就很容易识别错误。