Пример #1
0
        public ActionResult ExternalLoginCallback(string returnUrl)
        {
            var url = Url.Action("ExternalLoginCallback");



            AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl })); //facebook 2hrs token



            FacebookToken = result.ExtraData["accesstoken"];
            Provider      = result.Provider;
            if (Provider == "facebook")
            {
                string token = FacebookAPI.GetLongtermFbToken(FacebookToken);
                FacebookToken = token;
            }

            if (!result.IsSuccessful)
            {
                return(RedirectToAction("ExternalLoginFailure"));
            }

            if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
            {
                string            username  = OAuthWebSecurity.GetUserName(result.Provider, result.ProviderUserId);
                int               userId    = WebSecurity.GetUserId(username);
                FacebookScheduler scheduler = new FacebookScheduler();
                scheduler.RunScheduler(FacebookToken, userId);

                return(RedirectToLocal(returnUrl));
            }

            if (User.Identity.IsAuthenticated)
            {
                // If the current user is logged in add the new account
                DatabaseCallsApi _api = new DatabaseCallsApi();

                var username = OAuthWebSecurity.GetUserName(result.Provider, result.ProviderUserId);
                int user_id  = WebSecurity.GetUserId(username);
                _api.AddOrUpdateService(user_id, result.Provider, FacebookToken);
                OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, User.Identity.Name);

                return(RedirectToLocal(returnUrl));
            }
            else
            {
                // User is new, ask for their desired membership name



                string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId);
                ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(result.Provider).DisplayName;
                ViewBag.ReturnUrl           = returnUrl;
                return(View("ExternalLoginConfirmation", new RegisterExternalLoginModel {
                    UserName = result.UserName, ExternalLoginData = loginData
                }));
            }
        }
Пример #2
0
        public ActionResult FBAuth(string returnUrl)
        {
            var client      = new FacebookClient();
            var oauthResult = client.ParseOAuthCallbackUrl(Request.Url);

            // Build the Return URI form the Request Url
            var redirectUri = new UriBuilder(Request.Url);

            redirectUri.Path = Url.Action("FbAuth", "Account");
            dynamic result = client.Get("/oauth/access_token", new //get the facebook token
            {
                client_id     = Settings.Settings.FacebookAppId,
                redirect_uri  = Settings.Settings.FacebookCallbackURL,
                client_secret = Settings.Settings.FacebookAppSecret,
                code          = oauthResult.Code,
            });


            if (result == null)
            {
                return(RedirectToAction("ExternalLoginFailure"));
            }
            string accessToken = result.access_token;
            string token       = FacebookAPI.GetLongtermFbToken(accessToken); //get a 2month token

            FacebookToken = accessToken;
            Provider      = "facebook";
            dynamic me = client.Get("/me", //get some basic user info
                                    new
            {
                fields       = "first_name,last_name,email",
                access_token = accessToken
            });

            if (OAuthWebSecurity.Login("facebook", me.id, createPersistentCookie: false))
            {
                string            username  = OAuthWebSecurity.GetUserName("facebook", me.id);
                int               userId    = WebSecurity.GetUserId(username);
                FacebookScheduler scheduler = new FacebookScheduler(); //run any undone task
                scheduler.RunScheduler(token, userId);

                return(RedirectToLocal(returnUrl));
            }
            if (User.Identity.IsAuthenticated)
            {
                // If the current user is logged in add the new account
                DatabaseCallsApi _api = new DatabaseCallsApi();

                var username = OAuthWebSecurity.GetUserName("facebook", me.id);


                _api.AddOrUpdateService(WebSecurity.CurrentUserId, "facebook", token);
                OAuthWebSecurity.CreateOrUpdateAccount("facebook", me.id, WebSecurity.CurrentUserName.ToString());

                return(RedirectToLocal(returnUrl));
            }
            else
            {
                // User is new, ask for their desired membership name

                CheckChanceState();

                string loginData = OAuthWebSecurity.SerializeProviderUserId("facebook", me.id);
                ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData("facebook").DisplayName;
                ViewBag.ReturnUrl           = returnUrl;
                return(View("ExternalLoginConfirmation", new RegisterExternalLoginModel {
                    UserName = me.email, ExternalLoginData = loginData, Email = me.email
                }));
            }
        }