Example #1
0
        public ActionResult AuthenticatedWithFacebook()
        {
            var result = facebookClient.Authorize(string.Empty);

            if (result != LoginResult.Authorized)
            {
                return(RedirectToAction("Login"));
            }

            var facebookUser = facebookClient.GetCurrentUser();
            var user         = userService.GetUser(facebookUser.Id, LoginProvider.Facebook);

            if (user == null)
            {
                user = new User
                {
                    RemoteId      = facebookUser.Id,
                    UserName      = facebookUser.FirstName + " " + facebookUser.LastName,
                    LoginProvider = LoginProvider.Facebook,
                    Email         = facebookUser.Email,
                    FirstName     = facebookUser.FirstName,
                    LastName      = facebookUser.LastName
                };
                user.Id = userService.InsertUser(user, () => Redis.AddUser(user));
            }
            FormsAuthentication.SetAuthCookie(user.Id.ToString(), createPersistentCookie: true);

            var returnUrl = Request["state"];

            if (!returnUrl.IsNullOrEmpty())
            {
                Response.Redirect(returnUrl, true);
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }

            return(null);
        }