public async Task <bool> CreateB2CUser(UserModel userModel) { try { var uc = new GraphUserModel { accountEnabled = true }; List <SignInNames> names = new List <SignInNames>(); SignInNames name = new SignInNames { type = "emailAddress", value = userModel.email }; names.Add(name); uc.signInNames = names; uc.creationType = "LocalAccount"; uc.displayName = userModel.displayName; uc.passwordProfile = new PasswordProfile { password = userModel.password, forceChangePasswordNextLogin = false }; uc.passwordPolicies = "DisablePasswordExpiration"; var userString = Newtonsoft.Json.JsonConvert.SerializeObject(uc); await this._b2CGraphClient.CreateUser(userString); return(true); } catch (Exception ex) { return(false); } }
/// <summary> /// Create consumer user accounts /// When creating user accounts in a B2C tenant, you can send an HTTP POST request to the /users endpoint /// </summary> public async Task CreateUser(string signInName, string password, string displayName, string givenName, string surname, bool generateRandomPassword) { if (string.IsNullOrEmpty(signInName)) { throw new Exception("Email address is NULL or empty, you must provide valid email address"); } if (string.IsNullOrEmpty(displayName) || displayName.Length < 1) { throw new Exception("Dispay name is NULL or empty, you must provide valid dislay name"); } // Use random password for just-in-time migration flow if (generateRandomPassword) { password = GeneratePassword(); } try { // Create Graph json string from object var graphUserModel = new GraphUserModel(signInName, password, displayName, givenName, surname); // Send the json to Graph API end point await SendGraphRequest("/users/", null, graphUserModel.ToString(), HttpMethod.Post); Console.WriteLine($"Azure AD user account '{signInName}' created"); } catch (Exception ex) { if (ex.Message.Contains("ObjectConflict")) { // TBD: Add you error Handling here Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"User with same emaill address '{signInName}' already exists in Azure AD"); Console.ResetColor(); } } }