public IHttpActionResult PostNewUser(UserCreationModel newUser) { if (!newUser.Password.Equals(newUser.Repeat, StringComparison.InvariantCulture)) { return(BadRequest("Passwords do not match")); } var user = new List9User { Name = newUser.Name, UserName = newUser.UserName, Email = newUser.Email, PhoneNumber = newUser.PhoneNumber, Type = newUser.Type, }; var result = UserManager.Create(user, newUser.Password); if (result.Succeeded) { foreach (var role in newUser.Roles) { UserManager.AddToRole(user.Id, role); } if (!string.IsNullOrWhiteSpace(newUser.Email)) { //Validate Email } if (!string.IsNullOrWhiteSpace(newUser.PhoneNumber)) { //Validate Email } return(Ok(new { Name = user.Name, Type = user.Type, Email = user.Email, PhoneNumber = user.PhoneNumber, UserName = user.UserName, Roles = newUser.Roles, LockedOut = user.LockoutEnabled, EmailConfirmed = user.EmailConfirmed, PhoneNumberConfirmed = user.PhoneNumberConfirmed })); } else { return(BadRequest(String.Join(", ", result.Errors))); } }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { using (UserManager <List9User> userManager = _userManagerFactory()) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); List9User user = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user, context.Options.AuthenticationType); ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user, CookieAuthenticationDefaults.AuthenticationType); AuthenticationProperties properties = CreateProperties(user.UserName); AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); context.Validated(ticket); context.Request.Context.Authentication.SignIn(cookiesIdentity); } }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } List9User user = new List9User { UserName = model.UserName }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); IHttpActionResult errorResult = GetErrorResult(result); if (errorResult != null) { return(errorResult); } return(Ok()); }