public async Task<bool> Register(RegisterViewModel model) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) return false; await SignInManager.SignInAsync(user, false, false); return true; }
public string GenerateRandomPassword(RegisterViewModel model) { ApplicationDbContext context = new ApplicationDbContext(); string PasswordLength = "12"; string NewPassword = ""; string allowedChars = ""; allowedChars = "1,2,3,4,5,6,7,8,9,0"; allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,"; allowedChars += "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,"; allowedChars += "~,!,@,#,$,%,^,&,*,+,?"; char[] sep = { ',' }; string[] arr = allowedChars.Split(sep); string IDString = ""; string temp = ""; Random rand = new Random(); for (int i = 0; i < Convert.ToInt32(PasswordLength); i++) { temp = arr[rand.Next(0, arr.Length)]; IDString += temp; NewPassword = IDString; } // model.RandomPassword = NewPassword; model.Password = NewPassword; context.SaveChanges(); return NewPassword; }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.UserName, Email = model.Email }; string password = GenerateRandomPassword(model); var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //Assign Role to user Here await this.UserManager.AddToRoleAsync(user.Id, model.Name); //Ends Here string Email = user.Email; string sendEmail = ConfigurationManager.AppSettings["SendEmail"]; SendMail(Email, sendEmail, password); await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); return RedirectToAction("Index", "EmployeeInfo"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }