2017-05-07 23 views

回答

2

由於您已經成功地使用B2C圖形客戶端創建用戶,因此可以按照相同的方式發送多個創建用戶發佈請求以創建多個用戶。例如,您可以將數據庫中的用戶讀入數據表,然後循環數據表,初始化用戶對象併發送創建用戶請求。例如,參考this code sample,你可以嘗試下面的代碼來創建用戶:用戶實體的可選字段

private static void CreateUser() 
{ 

    //here you need to loop datatable and assign values 
    for (int i = 0; i < 2; i++) 
    { 
     UserData user = new UserData(); 
     user.accountEnabled = true; 
     SignInName sn = new SignInName(); 
     sn.type = "emailAddress"; 
     sn.value = "nanyuTest"+i+"@nanyutestb2c.onmicrosoft.com"; 
     user.signInNames = new List<SignInName>(); 
     user.signInNames.Add(sn); 
     user.creationType = "LocalAccount"; 
     user.displayName = "nan yu"; 
     user.mailNickname = "nanyuTest" + i; 
     PasswordProfile pf = new PasswordProfile(); 
     pf.password = "[email protected]!"; 
     pf.forceChangePasswordNextLogin = false; 
     user.passwordProfile = pf; 
     user.passwordPolicies = "DisablePasswordExpiration"; 
     string json = JsonConvert.SerializeObject(user); 
     object formatted = JsonConvert.DeserializeObject(client.CreateUser(json).Result); 

     Console.ForegroundColor = ConsoleColor.White; 
     Console.WriteLine(JsonConvert.SerializeObject(formatted, Formatting.Indented)); 
    } 
} 

    public class SignInName 
    { 
     public string type { get; set; } 
     public string value { get; set; } 
    } 

    public class PasswordProfile 
    { 
     public string password { get; set; } 
     public bool forceChangePasswordNextLogin { get; set; } 
    } 

    public class UserData 
    { 
     public bool accountEnabled { get; set; } 
     public List<SignInName> signInNames { get; set; } 
     public string creationType { get; set; } 
     public string displayName { get; set; } 
     public string mailNickname { get; set; } 
     public PasswordProfile passwordProfile { get; set; } 
     public string passwordPolicies { get; set; } 
     public string city { get; set; } 
     public object country { get; set; } 
     public object facsimileTelephoneNumber { get; set; } 
     public string givenName { get; set; } 
     public object mail { get; set; } 
     public object mobile { get; set; }   
     public string postalCode { get; set; } 
     public object preferredLanguage { get; set; } 
     public string state { get; set; } 
     public object streetAddress { get; set; } 
     public string surname { get; set; } 
     public object telephoneNumber { get; set; } 
    } 

詳細信息,您可以在Azure AD Graph API entity reference找到。

+0

在代碼示例中,我可以放置這些代碼嗎?我會替換當前的CreateUser()函數嗎?我不熟悉C#。我仍然會使用json文件來創建用戶嗎?如果是的話,我是否需要創建幾個json文件或者這個新函數是否有能力從json讀取數組?此函數是否需要與舊的CreateUser()函數不同的語法? –

+0

在代碼示例中,它從文件讀取json字符串,使用我的代碼可以將數據從數據庫讀取到數據表(可以在互聯網上搜索此部分代碼),然後循環數據表並將值分配給用戶實體併發送創建用戶(我的以上代碼)。在代碼示例中,您可以在下面註釋掉從文件讀取json的行:string json = File.ReadAllText(args [1]); –

+0

有沒有更新?如果您有任何問題,請隨時告訴我。 –

相關問題