示例#1
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password);
                    return RedirectToAction("Index", "Home");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
示例#2
0
 public UserInfo Register(RegisterModel model)
 {
     try
     {
         WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Email = model.Email });
         WebSecurity.Login(model.UserName, model.Password);
         Roles.AddUsersToRole(new string[] { model.UserName }, Settings.Default.DefaultRole);
         IPrincipal principal = new GenericPrincipal(new GenericIdentity(model.UserName), Roles.GetRolesForUser(model.UserName));
         Thread.CurrentPrincipal = principal;
         HttpContext.Current.User = principal;
         return new UserInfo()
         {
             IsAuthenticated = true,
             UserName = model.UserName,
             Roles = new List<string> { Settings.Default.DefaultRole }
         };
     }
     catch (MembershipCreateUserException e)
     {
         throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message));
     }
 }
示例#3
0
        public ActionResult JsonRegister(RegisterModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password);

                    FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
                    return Json(new { success = true, redirect = returnUrl });
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed
            return Json(new { errors = GetErrorsFromModelState() });
        }