RegisterNewUser() public method

public RegisterNewUser ( string userName, string password, bool partnerMail, string redirectUrl ) : long
userName string
password string
partnerMail bool
redirectUrl string
return long
        public virtual ActionResult ExternalLoginCallback(string returnUrl)
        {
            var ctx = new DatabaseDataContext();
            var bypass = ctx.GetSetting("ByPassPrefix");
            AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
            if (!result.IsSuccessful)
            {
                return RedirectToAction("ExternalLoginFailure");
            }
            var email = result.ExtraData["username"].ToLower();
            long id = -1;
            try
            {
                id = ctx.GetUserIDByEmail(email);
            }
            catch
            {

            }

            if (id == -1)
            {
                // new user should not get a confirmation mail
                try
                {
                    id = ctx.RegisterNewUser(
                        email,
                        Convert.ToBase64String(new SHA1CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(new Guid().ToString()))),
                        false,
                        returnUrl
                    );
                }
                catch
                {
                }
            }

            string token = "";
            if (id != -1)
            {
                try
                {
                    token = ctx.LoginUser(bypass + "_" + email, "notneeded", MvcApplication.HostIPAddress(HttpContext));
                }
                catch (Exception exception)
                {
                    switch (exception.ErrorCode())
                    {
                        case 60021:
                            ctx.ConfirmEmailAddress(Convert.ToBase64String(new SHA1CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(email))));
                            break;
                    }
                }
                // second try
                if (string.IsNullOrWhiteSpace(token))
                {
                    try
                    {
                        token = ctx.LoginUser(bypass + "_" + email, "notneeded", MvcApplication.HostIPAddress(HttpContext));
                    }
                    catch
                    {
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(token))
            {
                var tokenCookie = new HttpCookie("GameToken", token);
                tokenCookie.Expires = DateTime.Now.AddYears(1);
                Response.Cookies.Add(tokenCookie);
                if (!string.IsNullOrWhiteSpace(returnUrl))
                {
                    if (returnUrl.Contains("{guid}"))
                    {
                        if (returnUrl.Contains("?"))
                            returnUrl = returnUrl.Replace("{guid}", "&guid=" + token);
                        else
                            returnUrl = returnUrl.Replace("{guid}", "?guid=" + token);
                    }
                }
                else
                {
                    returnUrl = Url.Action(MVC.Home.Index());
                }
                ViewBag.ReturnUrl = returnUrl;
                return View(MVC.Home.Views.Shared.ExternalLoginCallback);
            }

            return RedirectToAction("ExternalLoginFailure");
        }
Exemplo n.º 2
0
        public virtual ActionResult Register(string emailAddress, string password, string language)
        {
            try
            {
                long? userID = null;
                var userName = emailAddress.Trim();
                password = password.Trim();
                // validate the email address
                try
                {
                    userName = NormalizeEmailAddress(userName);
                }
                catch
                {
                    throw new ApplicationException("60001 The supplied username is not a valid email address.");
                }
                // validate the password
                if (string.IsNullOrEmpty(password) | password.Length < 6)
                {
                    throw new ApplicationException("60002 The supplied password is empty or too short.");
                }
                // store in DB
                var confirmationpwd = GenerateConfirmationHash(userName);
                using (var ctx = new DatabaseDataContext())
                {
                    if (ctx.RegisterNewUser(userName, password, confirmationpwd, false, ref userID) == 0)
                        throw new ApplicationException("60003 Registering new user failed.");
                    // send a confirmation mail
                    try
                    {
                        SendConfirmationMail(new MailAddress(userName), userID, language);
                    }
                    catch (Exception e)
                    {
                        throw new ApplicationException("60005 Sending confirmation mail failed.", e);
                    }
                }
                if (!userID.HasValue)
                    throw new ApplicationException("60005 Sending confirmation mail failed.");
                //return userID.Value;
            }
            catch (Exception e)
            {
                EventLogger.WriteEvent(e.Message, EventLogger.EventType.Error, "Perpetuality");
            }

            return View(Views.RegisterThanks);
        }