public ActionResult Register(RegisterViewModel model, string returnUrl) { if(ModelState.IsValid) { if(model.Date.Month>12 || model.Date.Day>31 || model.Date.Year>(DateTime.Now.Year-16) ||model.Date.Year<DateTime.Now.Year-100) { ModelState.AddModelError("", "Неправильная дата!"); return View(model); } string role = ""; var result= UserManager.UserCreate(model,ref role); if(result==false) { ModelState.AddModelError("", "Пожалуйста введите другой email адрес"); return View(model); } var authTicket = new FormsAuthenticationTicket( 10, // Version model.Login, // User Login DateTime.Now, // Issue-Date DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes), // Expiration true, //TODO: Remember me role, // User Role FormsAuthentication.FormsCookiePath // Cookie Path ); var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket)); if (authTicket.IsPersistent) authCookie.Expires = authTicket.Expiration; Response.Cookies.Add(authCookie); return RedirectToAction("Index", "Profile"); } else { return View(model); } }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return RedirectToAction("Index", "Home"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }
public static bool UserCreate(RegisterViewModel model,ref string role) { if (model.Login == "admin123") role = "admin"; else role = "user"; if (MailMessanger.SendMessage("*****@*****.**", model.Email, "*****@*****.**","V80984342375")) { MD5 md5 = new MD5CryptoServiceProvider(); string pass = model.Password; byte[] checkSum = md5.ComputeHash(Encoding.UTF8.GetBytes(pass)); string result = BitConverter.ToString(checkSum).Replace("-", String.Empty); var user = new User() { FullName = model.SecondName+" "+model.FirstName+" "+model.ThirdName , HashPassword = result , Login = model.Login , Role = role , Email = model.Email , LastDate=DateTime.Now }; using (DatabaseContext db = new DatabaseContext()) { db.Users.Add(user); db.SaveChanges(); db.Profiles.Add(new Profile() { Id = user.Id, FirstName = model.FirstName, SecondName = model.SecondName, ThirdName = model.ThirdName, Date=model.Date, Gender=model.Gender, AvatarPath = "/Images/noavatar.png" }); db.SaveChanges(); } return true; } else { return false; } }