public async Task <ActionResult> CreateUser(b2c_ms_graph.UserModel userModel, FormCollection formCollection) { if (string.IsNullOrEmpty(userModel.newPassword)) { ModelState.AddModelError("", "New Password is required."); } if (userModel.newPassword != userModel.confirmPassword) { ModelState.AddModelError("", "New Password and confirm password do not match."); } if (ModelState.IsValid) { try { User newUser = new User(); IDictionary <string, object> extensionInstance = new Dictionary <string, object>(); extensionInstance.Add(B2cCustomAttributeHelper.GetCompleteAttributeName("WebRole"), int.Parse(ODataWebService.WebRole()) + 1); extensionInstance.Add(B2cCustomAttributeHelper.GetCompleteAttributeName("TenantId"), ODataWebService.TenantId()); extensionInstance.Add(B2cCustomAttributeHelper.GetCompleteAttributeName("CompanyId"), userModel.extension_39d2bd21d67b480891ffa985c6eb1398_CompanyId); extensionInstance.Add(B2cCustomAttributeHelper.GetCompleteAttributeName("CustomerNumber"), userModel.extension_39d2bd21d67b480891ffa985c6eb1398_CustomerNumber); newUser.AdditionalData = extensionInstance; newUser.DisplayName = userModel.DisplayName; newUser.AccountEnabled = userModel.DisplayAccountEnabled; newUser.PasswordProfile = new PasswordProfile { Password = userModel.newPassword, ForceChangePasswordNextSignIn = userModel.forcePasswordChange }; newUser.PasswordPolicies = "DisablePasswordExpiration"; newUser.Identities = new List <ObjectIdentity> { new ObjectIdentity { SignInType = "emailAddress", Issuer = "ICPCustomerPortal1.onmicrosoft.com", IssuerAssignedId = userModel.DisplayEmailName } }; GraphServiceClient graphClient = GraphClient.CreateGraphClient(); await graphClient.Users .Request() .AddAsync(newUser); return(RedirectToAction("Index")); } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); }; } return(View(userModel)); }
async Task <User> FindUserByADGuid(string userADGuid) { B2cCustomAttributeHelper helper = new B2cCustomAttributeHelper(this.b2cExtensionAppClientId); string adObjectGuidAttributeName = helper.GetCompleteAttributeName("ADObjectGUID"); try { // Get user by sign-in name var result = await this.graphClient.Users .Request() .Filter($"{adObjectGuidAttributeName} eq '{userADGuid}'") .Select($"id,givenName,surName,displayName,mail,identities") .GetAsync(); if (result != null) { // Yay, we found the user return(result[0]); } } catch (Exception ex) { // Note that we'll be in this block if the person isn't in B2C, // which could be because they're new. So this message is a warning, // not an error Log.Write(Log.Level.Warning, string.Format("Drat, got {0} when trying to find the B2C user with AD guid {1}, which is to be expected if they're new", ex.Message, userADGuid)); } return(null); }
private string getB2CAttributeNameFor(string attributeName) { string longName = ""; B2cCustomAttributeHelper helper = new B2cCustomAttributeHelper(this.b2cExtensionAppClientId); longName = helper.GetCompleteAttributeName(attributeName); return(longName); }
public async Task <ActionResult> UpdateUser(b2c_ms_graph.UserModel userModel, FormCollection formCollection) { if (userModel.newPassword != userModel.confirmPassword) { ModelState.AddModelError("confirmPassword", "New Password and confirm password do not match."); } if (ModelState.IsValid) { try { GraphServiceClient graphClient = GraphClient.CreateGraphClient(); User updateUser = new User(); IDictionary <string, object> extensionInstance = new Dictionary <string, object>(); extensionInstance.Add(B2cCustomAttributeHelper.GetCompleteAttributeName("CompanyId"), userModel.extension_39d2bd21d67b480891ffa985c6eb1398_CompanyId); extensionInstance.Add(B2cCustomAttributeHelper.GetCompleteAttributeName("CustomerNumber"), userModel.extension_39d2bd21d67b480891ffa985c6eb1398_CustomerNumber); updateUser.AdditionalData = extensionInstance; updateUser.DisplayName = userModel.DisplayName; updateUser.AccountEnabled = userModel.DisplayAccountEnabled; await graphClient.Users[userModel.Id] .Request() .UpdateAsync(updateUser); return(RedirectToAction("Index")); } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); }; } return(View(userModel)); }