Example #1
0
        public virtual ActionResult ExternalLoginCallback(string returnUrl)
        {
            Session["UserId"] = null;
            Session["PersonId"] = null;

            AuthenticationResult result = _oAuthProvider.VerifyOAuthAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
            if (!result.IsSuccessful)
            {
                return RedirectToAction("ExternalLoginFailure");
            }

            if (_oAuthProvider.OAuthLogin(result.Provider, result.ProviderUserId, persistCookie: false))
            {
                return RedirectToLocal(returnUrl);
            }

            if (User.Identity.IsAuthenticated)
            {
                // If the current user is logged in add the new account
                var user = new User()
                    {
                        Username = User.Identity.Name, Id = Guid.NewGuid().ToString()
                    };
                _oAuthProvider.CreateOAuthAccount(result.Provider, result.ProviderUserId, user);
                CreateOrUpdatePersonRecord(user.Username, user);
                return RedirectToLocal(returnUrl);
            }
            else
            {
                // User is new, ask for their desired membership name
                string loginData = _encoder.SerializeOAuthProviderUserId(result.Provider, result.ProviderUserId);
                ViewBag.ProviderDisplayName = _oAuthProvider.GetOAuthClientData(result.Provider).DisplayName;
                ViewBag.ReturnUrl = returnUrl;
                return View("ExternalLoginConfirmation", new RegisterExternalLoginModel { UserName = result.UserName, ExternalLoginData = loginData });
            }
        }
Example #2
0
        public virtual ActionResult Register(RegisterModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    var user = new User
                        {
                            Id = Guid.NewGuid().ToString(),
                            Username = model.EmailAddress.ToLower(),
                            Password = model.Password
                        };
                    CreateOrUpdatePersonRecord(model.Name, user);
                    _membershipProvider.CreateAccount(user);
                    _membershipProvider.Login(user.Username, model.Password);
                    Session["UserId"] = null;
                    Session["PersonId"] = null;
                    return RedirectToLocal(returnUrl);
                }
                catch (FlexMembershipException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Example #3
0
        private void CreateOrUpdatePersonRecord(string name, User user)
        {
            var person =
                RavenSession.Query<Person>()
                            .FirstOrDefault(
                                p => p.EmailAddress.Equals(user.Username, StringComparison.InvariantCultureIgnoreCase))
                ?? new Person
                    {
                        Id = Guid.NewGuid().ToString(),
                        EmailAddress = user.Username.ToLower(),
                        Name = name
                    };

            person.UserId = user.Id;
            RavenSession.Store(person);
        }
Example #4
0
        public virtual ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            Session["UserId"] = null;
            Session["PersonId"] = null;

            if (User.Identity.IsAuthenticated || !_encoder.TryDeserializeOAuthProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                if (!_membershipProvider.HasLocalAccount(model.UserName))
                {
                    var user = new User
                        {
                            Username = model.UserName, Id = Guid.NewGuid().ToString()
                        };
                    _oAuthProvider.CreateOAuthAccount(provider, providerUserId, user);
                    _oAuthProvider.OAuthLogin(provider, providerUserId, persistCookie: false);
                    CreateOrUpdatePersonRecord(user.Username, user);
                    return RedirectToLocal(returnUrl);
                }
                else
                {
                    ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                }
            }

            ViewBag.ProviderDisplayName = _oAuthProvider.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }