public async Task <IActionResult> Create([Bind("Id,SupplierId,FirstName,LastName,EmailAddress,AuthId,CreatedOn,CreatedBy,UpdatedOn,UpdatedBy")] User user) { var userFullName = User.Claims.FirstOrDefault(x => x.Type == $"name").Value; if (ModelState.IsValid) { if (User != null) { user.CreatedBy = userFullName; } user.CreatedOn = DateTime.Now; var graphClient = new GraphClient(Configuration, true); var password = string.Empty; var graphResult = graphClient.CreateUser(user, out password); if (graphResult) { _dbWriteService.Add(user); var saveResult = await _dbWriteService.SaveChangesAsync(); if (saveResult) { var userEmail = new PM.Business.Email.User(Configuration); userEmail.SendWelcomeEmail(user, password); return(RedirectToAction(nameof(Index))); } // Failed to save to the database, delete the user from the directory. graphClient.DeleteUser(user.AuthId); } } ViewData["SupplierId"] = new SelectList(_context.Supplier, "Id", "SupplierName", user.SupplierId); return(View(user)); }
public async Task OnExecute(IConsole console) { try { var sw = Stopwatch.StartNew(); var config = Config.GetConfig(); var client = new GraphClient(config, console); console.WriteLine($"creating user {displayName}..."); var user = await client.CreateUser(displayName, userPrincipalName, password); if (verboseFormat) { var str = JsonConvert.SerializeObject(user, Formatting.Indented); console.WriteLine(str); } else if (verbose) { var str = JsonConvert.SerializeObject(user, Formatting.None); console.WriteLine(str); } else { console.WriteLine($"displayName: {user["displayName"]}, objectId: {user["objectId"]}, userPrincipalName: {user["userPrincipalName"]}"); } if (!time) { return; } sw.Stop(); console.WriteLine($"operation completed in {sw.ElapsedMilliseconds}ms"); } catch (Exception ex) { console.WriteLine($"A {ex.GetType()} was caught: {ex.Message}"); } }