public async Task <IActionResult> Register([FromBody] JObject data) { var email = data.GetValue("Email", StringComparison.OrdinalIgnoreCase); var password = data.GetValue("Password", StringComparison.OrdinalIgnoreCase); if (email == null || password == null) { return(Error("Invalid email or password.")); } var user = new ApplicationUser { UserName = email.ToObject <string>(), Email = email.ToObject <string>() }; EntityPatch.Apply(user, data); OnUserCreated(user); var result = await userManager.CreateAsync(user, password.ToObject <string>()); if (result.Succeeded) { return(Created($"auth/Users('{user.Id}')", user)); } else { return(IdentityError(result)); } }
public async Task <IActionResult> Post([FromBody] JObject data) { var email = data.GetValue("Email", StringComparison.OrdinalIgnoreCase); var password = data.GetValue("Password", StringComparison.OrdinalIgnoreCase); if (email == null || password == null) { return(Error("Invalid email or password.")); } var user = new ApplicationUser { UserName = email.ToObject <string>(), Email = email.ToObject <string>() }; EntityPatch.Apply(user, data); OnUserCreated(user); IdentityResult result = null; result = await userManager.CreateAsync(user, password.ToObject <string>()); if (result.Succeeded) { var roles = data.GetValue("RoleNames"); if (roles != null) { var rolesData = roles.ToObject <IEnumerable <string> >(); if (rolesData.Any()) { result = await userManager.AddToRolesAsync(user, rolesData); if (!result.Succeeded) { return(IdentityError(result)); } } user.RoleNames = rolesData; } return(Created($"auth/Users('{user.Id}')", user)); } else { return(IdentityError(result)); } }
public async Task <IActionResult> Post([FromBody] System.Dynamic.ExpandoObject json) { var data = System.Text.Json.JsonSerializer.Deserialize <ApplicationUser>(System.Text.Json.JsonSerializer.Serialize(json)); var email = data.Email; var password = data.Password; var user = new ApplicationUser { UserName = email, Email = email }; EntityPatch.Apply(user, data); OnUserCreated(user); IdentityResult result = null; result = await userManager.CreateAsync(user, password); if (result.Succeeded) { var roles = data.RoleNames; if (roles != null && roles.Any()) { result = await userManager.AddToRolesAsync(user, roles); if (!result.Succeeded) { return(IdentityError(result)); } } user.RoleNames = roles; return(Created($"auth/Users('{user.Id}')", user)); } else { return(IdentityError(result)); } }
public async Task <IActionResult> Patch(string key, [FromBody] JObject data) { var user = await userManager.FindByIdAsync(key); if (user == null) { return(NotFound()); } EntityPatch.Apply(user, data); var roles = data.GetValue("RoleNames").ToObject <IEnumerable <string> >(); OnUserUpdated(user); IdentityResult result = null; if (roles != null) { result = await userManager.RemoveFromRolesAsync(user, await userManager.GetRolesAsync(user)); if (!result.Succeeded) { return(IdentityError(result)); } if (roles.Any()) { result = await userManager.AddToRolesAsync(user, roles); } if (!result.Succeeded) { return(IdentityError(result)); } } var password = data.GetValue("Password", StringComparison.OrdinalIgnoreCase).ToObject <string>(); if (password != null) { result = await userManager.RemovePasswordAsync(user); if (!result.Succeeded) { return(IdentityError(result)); } result = await userManager.AddPasswordAsync(user, password); if (!result.Succeeded) { return(IdentityError(result)); } } result = await userManager.UpdateAsync(user); if (!result.Succeeded) { return(IdentityError(result)); } return(new NoContentResult()); }
public async Task <IActionResult> Patch(string key, [FromBody] System.Dynamic.ExpandoObject json) { var user = await userManager.FindByIdAsync(key); if (user == null) { return(NotFound()); } var data = System.Text.Json.JsonSerializer.Deserialize <ApplicationUser>(System.Text.Json.JsonSerializer.Serialize(json)); var roles = data.RoleNames; OnUserUpdated(user); IdentityResult result = null; if (roles != null) { result = await userManager.RemoveFromRolesAsync(user, await userManager.GetRolesAsync(user)); if (!result.Succeeded) { return(IdentityError(result)); } if (roles.Any()) { result = await userManager.AddToRolesAsync(user, roles); } if (!result.Succeeded) { return(IdentityError(result)); } } var password = data.Password; if (!string.IsNullOrEmpty(password)) { result = await userManager.RemovePasswordAsync(user); if (!result.Succeeded) { return(IdentityError(result)); } result = await userManager.AddPasswordAsync(user, password); if (!result.Succeeded) { return(IdentityError(result)); } } EntityPatch.Apply(user, data); result = await userManager.UpdateAsync(user); if (!result.Succeeded) { return(IdentityError(result)); } return(new NoContentResult()); }