Ejemplo n.º 1
0
 /// <summary>
 /// Extension transformate User to RegistrationViewModel object
 /// </summary>
 /// <param name="user">User DTO object</param>
 /// <returns>RegistrationViewModel object which contains data from User DTO object</returns>
 public static RegistrationViewModel ToViewModel(this User user)
 {
     RegistrationViewModel userViewModel = null;
     if (user != null)
     {
         userViewModel = new RegistrationViewModel();
         userViewModel.Id = user.Id;
         userViewModel.FirstName = user.FirstName;
         userViewModel.MiddleName = user.MiddleName;
         userViewModel.LastName = user.LastName;
         userViewModel.Email = user.Email;
         userViewModel.Phone = user.Phone;
         userViewModel.Login = user.Login;
         userViewModel.Password = user.Password;
         userViewModel.ConfirmPassword = user.Password;
     }
     return userViewModel;
 }
Ejemplo n.º 2
0
        public ActionResult Registration(RegistrationViewModel registrationViewModel)
        {
            if (UserRepository.IsLoginExists(registrationViewModel.Login))
            {
                ModelState.AddModelError("Login", Error.CurrentLoginExists);
            }

            if (!string.Equals(registrationViewModel.Password, registrationViewModel.ConfirmPassword))
            {
                ModelState.AddModelError("Password", Error.PasswordsMismatch);
            }

            if (ModelState.IsValid)
            {
                var user = registrationViewModel.ToEntity();

                //Save / Register user
                var response = UserRepository.Add(SessionVars.UserId, user);
                user.Id = response.Id;

                TagRepository.Add(user.Id, new Tag { Name = "ExpensesTag" });
                var ticket = new FormsAuthenticationTicket(1, user.Login, DateTime.Now, DateTime.Now.AddSeconds(1800), false, "SimpleUser"); //TODO: Implement more rolles if it will be needed for the task, or I could user MemberShip provide. Just wanted to show my experience with authentication.
                var strEncryptedTicket = FormsAuthentication.Encrypt(ticket);
                var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, strEncryptedTicket);
                Response.Cookies.Add(cookie);

                SessionVars.UserId = user.Id;
                SessionVars.UserName = string.Format("{0} {1} {2}", user.FirstName, user.MiddleName, user.LastName).Replace("  ", " "); //The last replacing will work when user don't have middle name. It will correct full name format.
                return RedirectToAction("GetTagsTree", "Tag");
            }
            else
            {
                return View(registrationViewModel);
            }
        }