public async Task <ActionResult> SignUp(RegistrationVm registrationVm) { string messageRegistration = string.Empty; if (ModelState.IsValid) { //Login Verification var userDto = await Task.Run(() => _serviceFacade.UserService.Get.ByLogin(registrationVm.Login)); if (userDto != null) { ModelState.AddModelError("Warning key", "This login already exists"); } //Create user UserDto newUser = Mapper.Map <RegistrationVm, UserDto>(registrationVm); _serviceFacade.UserService.CreateAccount(newUser); MySocNetMembershipUser membershipUser = new MySocNetMembershipUser(newUser); SignIn(membershipUser); //redirecting return(JavaScript($"window.location = '{Url.Action("Settings", "User", new { id = membershipUser.UserId })}'")); } else { messageRegistration = "Не все поля заполнены корректно!"; } ViewBag.RegistrationErrorMessage = messageRegistration; return(View(registrationVm)); }
/// <summary> /// Creates FormsAuthenticationTicket and HttpCookie authCookie. Adds them to HttpResponse /// </summary> /// <param name="user"></param> private void SignIn(MySocNetMembershipUser user) { MembershipUserSerializeModel serializeModel = new MembershipUserSerializeModel() { UserId = user.UserId, Login = user.Login, Roles = user.Roles }; string userData = System.Web.Helpers.Json.Encode(serializeModel); FormsAuthenticationTicket authenticationTicket = new FormsAuthenticationTicket (1, user.Login, DateTime.Now, DateTime.Now.AddMinutes(30), true, userData); string enTicket = FormsAuthentication.Encrypt(authenticationTicket); HttpCookie authCookie = new HttpCookie(Cookies.AuthentificationCookieName, enTicket); Response.Cookies.Add(authCookie); }