/// <summary> /// This will generate a new company from a view company /// </summary> /// <param name="comp">the company to be generated</param> /// <returns>the company that should be added to the database</returns> public Company GenerateCompany(ViewCompany comp, int _lenghtOfPassword, int _numberOfAlphabeticCharacters) { var result = new Company() {Active = true, Name = comp.Name, PhoneNr = comp.PhoneNr}; //Generates a random password, and makes a new user with the correct email var password = System.Web.Security.Membership.GeneratePassword(_lenghtOfPassword, _numberOfAlphabeticCharacters); var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new DAL.Context.Context())); var user = new ApplicationUser() { UserName = comp.Email, Email = comp.Email }; um.Create(user, password); //Generates the access string by encoding the email and the password into an array of bytes. var bytes = System.Text.Encoding.UTF8.GetBytes(comp.Email + ":" + password); result.AccessString = System.Convert.ToBase64String(bytes); result.Active = true; //This will be so that the database will make the relation between the two. var item = um.FindByEmail(comp.Email); result.IdentityId = item.Id; return result; }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email.Substring(0, model.Email.IndexOf("@")), Email = model.Email, FirstName = model.FirstName, LastName = model.LastName }; var result = await UserManager.CreateAsync(user, model.Password); UserManager.AddToRole(user.Id, "user"); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); return RedirectToAction("Index", "Home"); } AddErrors(result); } // Появление этого сообщения означает наличие ошибки; повторное отображение формы return View(model); }