0
我想要一個c#.net中的示例應用程序,它可以使用Microsoft api在Office 365中創建用戶。任何人都請幫助。我希望在代碼中完成,而不是使用電源外殼。在Office 365中使用c#.net代碼動態創建用戶帳戶
我想要一個c#.net中的示例應用程序,它可以使用Microsoft api在Office 365中創建用戶。任何人都請幫助。我希望在代碼中完成,而不是使用電源外殼。在Office 365中使用c#.net代碼動態創建用戶帳戶
您可以使用微軟圖形API - Create User:
在Azure AD註冊本地客戶端應用程序,分配「微軟圖形」>「讀取和寫入目錄數據」的權限。
string authority = "https://login.windows.net/yourdomain.onmicrosoft.com";
string clientId = "{app_client_id}";
Uri redirectUri = new Uri("http://localhost");
string resourceUrl = "https://graph.microsoft.com";
HttpClient client = new HttpClient();
AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);
AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resourceUrl,
clientId, redirectUri, PromptBehavior.Always);
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + authenticationResult.AccessToken);
string content = @"{
'accountEnabled': true,
'displayName': 'testuser',
'mailNickname': 'test',
'passwordProfile': {
'forceChangePasswordNextSignIn': true,
'password': '[email protected]'
},
'userPrincipalName': '[email protected]'
}";
var httpContent = new StringContent(content, Encoding.GetEncoding("utf-8"), "application/json");
var response = client.PostAsync("https://graph.microsoft.com/v1.0/users", httpContent).Result;
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
類型「System.FormatException」的未處理的異常發生在Microsoft.IdentityModel.Clients.ActiveDirectory.dll – bijupranavam
其他信息:索引(基於零)必須大於或等於零,並且小於參數列表的大小。 – bijupranavam
請確保您將「app_client_id」替換爲實際的應用客戶端ID(例如dcd68e75-54d4-xxxx-9dfb-xxxx3833ec1a,沒有「{」和「}」)。並確保使用您的實際域名替換「yourdomain」。 –