public async Task <IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = new ApplicationUser { UserName = createUserModel.Username, Email = createUserModel.Email, FirstName = createUserModel.FirstName, LastName = createUserModel.LastName, Level = 3, JoinDate = DateTime.Now.Date, }; IdentityResult addUserResult = await AppUserManager.CreateAsync(user, createUserModel.Password); if (!addUserResult.Succeeded) { return(GetErrorResult(addUserResult)); } string code = await AppUserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code })); await AppUserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id })); return(Created(locationHeader, TheModelFactory.Create(user))); }
public async Task <IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = new ApplicationUser() { UserName = createUserModel.Username, Email = createUserModel.Email, FirstName = createUserModel.FirstName, LastName = createUserModel.LastName, Level = 3, JoinDate = DateTime.Now.Date, }; IdentityResult addUserResult = await this.AppUserManager.CreateAsync(user, createUserModel.Password); if (!addUserResult.Succeeded) { return(GetErrorResult(addUserResult)); } Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id })); return(Created(locationHeader, TheModelFactory.Create(user))); }
private string ValidateCreateModel(CreateUserBindingModel createModel, Role role) { string msg = String.Empty; if (role.Equals(Role.Affiliate)) { int category; if (!int.TryParse(createModel.UserCategory, out category)) { msg = "That is not a valid user category"; ModelState.AddModelError("createUserModel.UserCategory", msg); } } if (role.Equals(Role.Vendor)) { if (String.IsNullOrEmpty(createModel.ProgramDescription)) { msg = "Please enter a Program Description"; ModelState.AddModelError("createUserModel.ProgramDescription", msg); } if (String.IsNullOrEmpty(createModel.ProgramName)) { msg = "Please enter a Program Name"; ModelState.AddModelError("createUserModel.ProgramName", msg); } Program program = MarketManager.GetAllPrograms().Where(p => p.Name.ToLower() == createModel.ProgramName.ToLower()).FirstOrDefault(); if (null != program) { msg = "That program name is already taken, please enter something different"; ModelState.AddModelError("createUserModel.ProgramName", msg); } int category; if (!int.TryParse(createModel.ProgramCategory, out category)) { msg = "That is not a valid program category"; ModelState.AddModelError("createUserModel.ProgramCategory", msg); } if (String.IsNullOrEmpty(createModel.ProgramUrl)) { msg = "Please enter a Program Url"; ModelState.AddModelError("createUserModel.ProgramUrl", msg); } } return(msg); }
public async Task <IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = new ApplicationUser() { UserName = createUserModel.Username, Email = createUserModel.Email, FirstName = createUserModel.FirstName, LastName = createUserModel.LastName, Level = 3, JoinDate = DateTime.Now.Date, isActive = true, }; IdentityResult addUserResult = await this.AppUserManager.CreateAsync(user, createUserModel.Password); if (!addUserResult.Succeeded) { return(GetErrorResult(addUserResult)); } var modelId = createUserModel.Role? "ca67b386-48db-4eb6-a67e-29c8eb5af895" : "c361cb50-97c6-40d1-ab1b-414ceeefcba7"; var role = await this.AppRoleManager.FindByIdAsync(modelId); string code = await this.AppUserManager.GenerateEmailConfirmationTokenAsync(user.Id); IdentityResult result = await this.AppUserManager.AddToRoleAsync(user.Id, role.Name); //var callbackUrl = new Uri("http://localhost:4200/confirmemail?userId=" + Uri.EscapeDataString(user.Id) + "&code=" + Uri.EscapeDataString(code)); //var callbackUrl2 = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code })); //await this.AppUserManager.SendEmailAsync(user.Id, // "Confirm your account", // "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a> and" // + callbackUrl2); Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id })); return(Created(locationHeader, TheModelFactory.Create(user))); }
public async Task <IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } bool IsMarketer = false; if (null != createUserModel.Marketer && !bool.TryParse(createUserModel.Marketer, out IsMarketer)) { return(BadRequest()); } bool IsAffiliate = false; if (null != createUserModel.Affiliate && !bool.TryParse(createUserModel.Affiliate, out IsAffiliate)) { return(BadRequest()); } if (!IsMarketer && !IsAffiliate) { return(BadRequest("Affiliate or Marketer must be selected")); } if (IsAffiliate) { string error = ValidateCreateModel(createUserModel, Role.Affiliate); if (!String.IsNullOrEmpty(error)) { return(BadRequest(ModelState)); } } // check if the user created a program if (IsMarketer) { string error = ValidateCreateModel(createUserModel, Role.Vendor); if (!String.IsNullOrEmpty(error)) { return(BadRequest(ModelState)); } } var user = new ApplicationUser() { UserName = createUserModel.Username, Email = createUserModel.Email, FirstName = createUserModel.FirstName, LastName = createUserModel.LastName, Level = 3, JoinDate = DateTime.Now.Date }; IdentityResult addUserResult = await this.AppUserManager.CreateAsync(user, createUserModel.Password); if (!addUserResult.Succeeded) { return(GetErrorResult(addUserResult)); } // extend the user with specific information var userExt = new UserExtension() { SkypeHandle = createUserModel.SkypeHandle, UserId = user.Id, IndividualDescription = createUserModel.IndividualDescription, FirstName = createUserModel.FirstName, LastName = createUserModel.LastName, PhoneNumber = createUserModel.PhoneNumber, Category = Convert.ToInt32(createUserModel.UserCategory) }; UserExtManager.UserExtensions.Add(userExt); try { int resultCount = await UserExtManager.Update(); } catch (Exception ex) { // todo delete user here try { await AppUserManager.DeleteAsync(user); } catch { // do our best to not create secondary errors } return(InternalServerError()); } // check if the user created a program if (IsMarketer) { AppUserManager.AddToRole(user.Id, "Vendor"); Program newProgram = new Program() { CreatedDate = DateTime.Now, CreatorId = user.Id, Description = createUserModel.ProgramDescription, Url = createUserModel.ProgramUrl, Name = createUserModel.ProgramName, Category = Convert.ToInt32(createUserModel.ProgramCategory) }; MarketManager.Programs.Add(newProgram); await MarketManager.Update(); } if (IsAffiliate) { AppUserManager.AddToRole(user.Id, "Affiliate"); } Uri locationHeader = await SendConfirm(user); return(Created(locationHeader, TheModelFactory.Create(user, userExt))); }