Example #1
0
        public ActionResult Register(CreateUserModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    int retVal = _accountBl.CreateUser(model);
                    switch (retVal)
                    {
                    case 0:
                        logger.Info("User created. Username = "******""
                        }
                                                                        );
                        HttpContext.User = new UserPrincipal(sessionUser);
                        FormsAuthentication.SetAuthCookie(sessionUser.Username, false);
                        var authTicket = new FormsAuthenticationTicket(
                            1,
                            sessionUser.Username,
                            DateTime.Now,
                            DateTime.Now.AddMinutes(60),
                            false,
                            sessionUser.Role
                            );
                        string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                        var    authCookie      = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                        HttpContext.Response.Cookies.Add(authCookie);
                        return(RedirectToAction("Index", "Auctions"));

                    case -1:
                        logger.Info("Username " + model.Username + " already exists.");
                        ModelState.AddModelError("Username", "Specified username is already taken.");
                        return(View());

                    case -2:
                        logger.Info("Email " + model.Email + " already exists.");
                        ModelState.AddModelError("Email", "Specified email address already exists in the system.");
                        return(View());
                    }
                }
                logger.Info("Invalid model state.");
                return(View());
            }
            catch (Exception e)
            {
                logger.Error("Exception occured, redirecting to registration page." + e.Message);
                return(View());
            }
        }