public async Task <string> CreateUser(GraphObject graphObject) { var client = new B2CGraphClient(clientId, clientSecret, tenant); var response = await client.CreateUser(graphObject.UserJsonData); return(response); }
public async Task CreateUserTest() { var client = new B2CGraphClient("aeefcc33-b5b3-4997-90e9-817e0d91d068", Secret, "b6bdfb2f-60a9-48b4-9fa5-5c15a97e4ffb"); var userTemplate = JObject.Parse(new StreamReader(typeof(B2CGraphClient).Assembly.GetManifestResourceStream("IdentityServer.AzureAdUserService.CreateUserTemplate.json")).ReadToEnd()); (userTemplate.SelectToken("alternativeSignInNamesInfo[0].type").Parent as JProperty).Value = "userName"; // "emailAddress"; (userTemplate.SelectToken("alternativeSignInNamesInfo[0].value").Parent as JProperty).Value = "myUserName"; (userTemplate.SelectToken("passwordProfile.password").Parent as JProperty).Value = "P@ssword!"; Console.WriteLine(JObject.Parse(await client.CreateUser(userTemplate.ToString())).ToString(Formatting.Indented)); }
private async Task <ADUser> CreateUserAD(UserDto user) { //Obtain B2C Settings var b2cDB = new ADB2CSettings() { Id = 1 }; var b2cSettings = await _db.GetAsync(b2cDB); //Create a new user object var userObject = new JObject { { "accountEnabled", true }, { "creationType", "LocalAccount" }, { "displayName", user.FullName }, { "passwordProfile", new JObject { { "password", "WSXzaq!23" }, { "forceChangePasswordNextLogin", true } } }, { "signInNames", new JArray { new JObject { { "type", "emailAddress" }, { "value", user.Email.Trim() } } } } }; //Use Microsoft Graph to perform action on Azure AD B2C var client = new B2CGraphClient( b2cSettings.AadClientId, b2cSettings.AadClientSecret, b2cSettings.AadTenant, b2cSettings.AadGraphResourceId, b2cSettings.AadGraphEndpoint, b2cSettings.AadGraphVersion); var response = await client.CreateUser(userObject.ToString()); var newUser = JsonConvert.DeserializeObject <ADUser>(response); return(newUser); }
public async Task <IActionResult> Register([FromBody] RegisterViewModel model) { if (ModelState.IsValid) { var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Icr78VL86Up+ZxLdv+OR5aQVKov2rsg9wzVfXKZpbAg="); /* OAuth2 is required to access this API. For more information visit: * https://msdn.microsoft.com/en-us/office/office365/howto/common-app-authentication-tasks */ // Specify values for path parameters (shown as {...}) var uri = "https://graph.windows.net/chsakellhotmail.onmicrosoft.com/users?api-version=1.6"; var jsonObject = new JObject { { "accountEnabled", true }, { "creationType", "LocalAccount" }, { "displayName", model.Username.Trim() }, { "passwordPolicies", "DisablePasswordExpiration,DisableStrongPassword" }, { "passwordProfile", new JObject { { "password", model.Password }, { "forceChangePasswordNextLogin", false } } }, { "signInNames", new JArray { new JObject { { "value", model.Email.Trim() }, { "type", "emailAddress" } } } } }; try { var b2cClient = new B2CGraphClient(ClientId, ClientSecret, TenantId); var response = await b2cClient.CreateUser(jsonObject.ToString()); var adUser = JsonConvert.DeserializeObject <ActiveDirectoryUser>(response); if (adUser != null) { return(Ok(new ResultViewModel() { Result = Result.SUCCESS, Data = new { username = adUser.SignInNames[0].Value, id = adUser.ObjectId, redirect = true } })); } else { return(Ok(new ResultViewModel() { Result = Result.ERROR, Message = "Something went wrong" })); } } catch (Exception ex) { } } return(BadRequest(new ResultViewModel() { Result = Result.ERROR, Message = "Bad request" })); }