public async Task <GraphAccountModel> GetUserByObjectId(string objectId) { GraphAccountModel graphAccountModel = null; string user = await SendGraphRequest($"/users/{objectId}", null, null, HttpMethod.Get); graphAccountModel = GraphAccountModel.Parse(user); return(graphAccountModel); }
public async Task UpdateUser(string objectId, string displayName, string givenName, string surname, string extension_Organization, string extension_UserRole) { GraphAccountModel user = await this.GetUserByObjectId(objectId); if (user != null) { GraphUserUpdateModel graphUserUpdateModel = new GraphUserUpdateModel(displayName, givenName, surname, extension_Organization, extension_UserRole); string json = graphUserUpdateModel.ToString(); await SendGraphRequest($"/users/{user.objectId}", null, json, new HttpMethod("PATCH")); } }
public async Task <GraphAccountModel> GetUser(string signInName) { GraphAccountModel graphAccountModel = null; string JSON = await this.SearchUserBySignInNames(signInName); GraphAccounts users = GraphAccounts.Parse(JSON); // If user exists if (users != null && users.value != null && users.value.Count == 1) { string user = await SendGraphRequest($"/users/{users.value[0].objectId}", null, null, HttpMethod.Get); graphAccountModel = GraphAccountModel.Parse(user); } return(graphAccountModel); }
public async Task <GraphAccountModel> GetUserByUserPrincipalName(string userPrincipalName) { GraphAccountModel graphAccountModel = null; string JSON = await SendGraphRequest("/users/", $"$filter=userPrincipalName eq '{userPrincipalName}'", null, HttpMethod.Get); GraphAccounts users = GraphAccounts.Parse(JSON); // If user exists if (users != null && users.value != null && users.value.Count == 1) { string user = await SendGraphRequest($"/users/{users.value[0].objectId}", null, null, HttpMethod.Get); graphAccountModel = GraphAccountModel.Parse(user); } return(graphAccountModel); }
/// <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 <GraphAccountModel> CreateAccount( string userType, string signInName, string issuer, string issuerUserId, string email, string password, string displayName, string givenName, string surname, string extension_Organization, string extension_UserRole, bool generateRandomPassword) { if (string.IsNullOrEmpty(signInName) && string.IsNullOrEmpty(issuerUserId)) { throw new Exception("You must provide user's signInName or issuerUserId"); } 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 GraphAccountModel graphUserModel = new GraphAccountModel( Tenant, userType, signInName, issuer, issuerUserId, email, password, displayName, givenName, surname, extension_Organization, extension_UserRole); // Send the json to Graph API end point string JSON = await SendGraphRequest("/users/", null, graphUserModel.ToString(), HttpMethod.Post); GraphAccountModel newUser = GraphAccountModel.Parse(JSON); Console.WriteLine($"Azure AD user account '{displayName}' created"); return(newUser); } catch (Exception ex) { if (ex.Message.Contains("ObjectConflict")) { // TBD: Add you error Handling here Console.ForegroundColor = ConsoleColor.Red; if (ex.Message.Contains("signInNames ")) { Console.WriteLine($"User with same signInNames '{signInName}' already exists in Azure AD"); } else if (ex.Message.Contains("userIdentities ")) { Console.WriteLine($"User with same userIdentities '{issuerUserId}' already exists in Azure AD"); } else if (ex.Message.Contains("one or more")) { Console.WriteLine($"User with same userIdentities '{issuerUserId}', and signInNames '{signInName}' already exists in Azure AD"); } Console.ResetColor(); } return(null); } }