Esempio n. 1
0
        public string Register(RegisterModel model)
        {
            // BUGBUG: access to this API must be controlled - e.g. the client must send a secret
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(
                        model.UserName,
                        model.Password,
                        propertyValues: new
                    {
                        Name = model.Name,
                    },
                        requireConfirmationToken: false);
                    WebSecurity.Login(model.UserName, model.Password);
                    TraceLog.TraceInfo(string.Format("Created user {0}", model.UserName));

                    using (var repository = new UserDataRepository(model.UserName))
                    {
                        repository.InitializeNewUserAccount();
                    }

                    var sentMail = EmailProcessor.SendWelcomeEmail(model.UserName);
                    if (sentMail)
                    {
                        TraceLog.TraceInfo("Sent welcome email to user " + model.UserName);
                    }

                    FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
                    return(model.UserName);
                }
                catch (MembershipCreateUserException ex)
                {
                    TraceLog.TraceException(string.Format("Could not create user {0}", model.UserName), ex);
                    return("Error: " + ex.Message);
                }
                catch (Exception ex)
                {
                    TraceLog.TraceException(string.Format("Could not create user {0}", model.UserName), ex);
                }
            }

            // If we got this far, something failed
            return(null);
        }
Esempio n. 2
0
        public ActionResult JsonRegister(RegisterModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(
                        model.UserName,
                        model.Password,
                        propertyValues: new
                    {
                        Name = model.Name,
                    },
                        requireConfirmationToken: false);
                    WebSecurity.Login(model.UserName, model.Password);
                    TraceLog.TraceInfo(string.Format("Created user {0}", model.UserName));

                    using (var repository = new UserDataRepository(model.UserName))
                    {
                        repository.InitializeNewUserAccount();
                    }

                    var sentMail = EmailProcessor.SendWelcomeEmail(model.UserName);
                    if (sentMail)
                    {
                        TraceLog.TraceInfo("Sent welcome email to user " + model.UserName);
                    }

                    FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
                    return(Json(new { success = true, redirect = returnUrl }));
                }
                catch (MembershipCreateUserException e)
                {
                    var error = ErrorCodeToString(e.StatusCode);
                    ModelState.AddModelError("", error);
                    TraceLog.TraceException(string.Format("Account creation failed: {0}", error), e);
                }
                catch (Exception ex)
                {
                    TraceLog.TraceException("Account creation failed", ex);
                }
            }

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