Пример #1
0
        public async Task <ActionResult> Complete()
        {
            var auth = new MvcAuthorizer
            {
                CredentialStore = new SessionStateCredentialStore(HttpContext.Session)
            };

            await auth.CompleteAuthorizeAsync(new Uri(Request.GetDisplayUrl()));

            // This is how you access credentials after authorization.
            // The oauthToken and oauthTokenSecret do not expire.
            // You can use the userID to associate the credentials with the user.
            // You can save credentials any way you want - database,
            //   isolated storage, etc. - it's up to you.
            // You can retrieve and load all 4 credentials on subsequent
            //   queries to avoid the need to re-authorize.
            // When you've loaded all 4 credentials, LINQ to Twitter will let
            //   you make queries without re-authorizing.
            //
            //var credentials = auth.CredentialStore;
            //string oauthToken = credentials.OAuthToken;
            //string oauthTokenSecret = credentials.OAuthTokenSecret;
            //string screenName = credentials.ScreenName;
            //ulong userID = credentials.UserID;
            //

            return(RedirectToPage("Index"));
        }
Пример #2
0
        /// <summary>
        /// Get twitter oauth tokens
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public dynamic GetTwitterTokens(Uri uri)
        {
            var auth = new MvcAuthorizer
            {
                CredentialStore = new SessionStateCredentialStore()
            };
            Task task = auth.CompleteAuthorizeAsync(uri);

            task.Wait(3000);
            return(auth);
        }
        public async Task <ActionResult> CompleteAsync()
        {
            var auth = new MvcAuthorizer
            {
                CredentialStore = new SessionStateCredentialStore()
            };
            await auth.CompleteAuthorizeAsync(Request.Url);

            var authCredentials = auth.CredentialStore;

            // É assim que você acessa as credenciais após a autorização.
            // O oauthToken e oauthTokenSecret não expiram.
            // Você pode usar o userID para associar as credenciais ao usuário.
            // Você pode salvar credenciais da maneira que quiser - banco de dados,
            // armazenamento isolado, etc. - depende de você.
            // Você pode recuperar e carregar todas as 4 credenciais em subseqüentes
            // consultas para evitar a necessidade de autorizar novamente.
            // Quando você tiver carregado todas as 4 credenciais, o LINQ to Twitter permitirá
            // você fazer consultas sem re-autorizar.
            //================================================================================

            Credenciais credenciais = new Credenciais
            {
                ConsumerKey      = authCredentials.ConsumerKey,
                ConsumerSecret   = authCredentials.ConsumerSecret,
                OAuthToken       = authCredentials.OAuthToken,
                OAuthTokenSecret = authCredentials.OAuthTokenSecret,
                ScreenName       = authCredentials.ScreenName,
                UserID           = authCredentials.UserID
            };

            // Carreegando propriedades static da class CredenciaisAuth
            // ========================================================
            CredenciaisAuth.ConsumerKey      = authCredentials.ConsumerKey;
            CredenciaisAuth.ConsumerSecret   = authCredentials.ConsumerSecret;
            CredenciaisAuth.OAuthToken       = authCredentials.OAuthToken;
            CredenciaisAuth.OAuthTokenSecret = authCredentials.OAuthTokenSecret;
            CredenciaisAuth.ScreenName       = authCredentials.ScreenName;
            CredenciaisAuth.UserID           = authCredentials.UserID;

            //Carreegando propriedades num Cokie
            //==================================
            HttpCookie httpcookie = new HttpCookie("twitterdesk");

            httpcookie.Value   = credenciais.ToString();
            httpcookie.Expires = DateTime.Now.AddMonths(1);
            Response.Cookies.Add(httpcookie);

            return(RedirectToAction("Index", "StatusDemos"));
        }
Пример #4
0
        //
        // GET: /Account/LogOn
        public async Task <ActionResult> LogOn(bool complete = false)
        {
            //return View();
            if (!Request.IsAuthenticated)
            {
                if (complete)
                {
                    var auth = new MvcAuthorizer
                    {
                        CredentialStore = new SessionStateCredentialStore()
                    };

                    await auth.CompleteAuthorizeAsync(Request.Url);

                    FormsAuthentication.SetAuthCookie(auth.CredentialStore.ScreenName, true);
                    PostworthyUser pm = UsersCollection.Single(auth.CredentialStore.ScreenName, force: true, addIfNotFound: true);
                    if (string.IsNullOrEmpty(pm.AccessToken) && string.IsNullOrEmpty(pm.OAuthToken))
                    {
                        pm.AccessToken = auth.CredentialStore.OAuthTokenSecret;
                        pm.OAuthToken  = auth.CredentialStore.OAuthToken;
                        UsersCollection.Save();
                    }

                    return(RedirectToAction("Index", new { controller = "Home", action = "Index", id = UrlParameter.Optional }));
                }
                else
                {
                    //var auth = new MvcSignInAuthorizer
                    var auth = new MvcAuthorizer
                    {
                        CredentialStore = new SessionStateCredentialStore
                        {
                            ConsumerKey    = ConfigurationManager.AppSettings["TwitterCustomerKey"],
                            ConsumerSecret = ConfigurationManager.AppSettings["TwitterCustomerSecret"]
                        }
                    };
                    string twitterCallbackUrl = Request.Url.ToString() + "?complete=true";
                    return(await auth.BeginAuthorizationAsync(new Uri(twitterCallbackUrl)));
                }
            }
            else
            {
                return(RedirectToAction("Index", new { controller = "Home", action = "Index", id = UrlParameter.Optional }));
            }
        }
Пример #5
0
        public async Task CompleteAuthorizeAsync(ISession session, string responseUrl)
        {
            MvcAuthorizer auth = GetAuthorizer(session);

            await auth.CompleteAuthorizeAsync(new Uri(responseUrl));

            // This is how you access credentials after authorization.
            // The oauthToken and oauthTokenSecret do not expire.
            // You can use the userID to associate the credentials with the user.
            // You can save credentials any way you want - database,
            //   isolated storage, etc. - it's up to you.
            // You can retrieve and load all 4 credentials on subsequent
            //   queries to avoid the need to re-authorize.
            // When you've loaded all 4 credentials, LINQ to Twitter will let
            //   you make queries without re-authorizing.
            //
            //var credentials = auth.CredentialStore;
            //string oauthToken = credentials.OAuthToken;
            //string oauthTokenSecret = credentials.OAuthTokenSecret;
            //string screenName = credentials.ScreenName;
            //ulong userID = credentials.UserID;
        }
Пример #6
0
        public async Task <ActionResult> CompleteAsync()
        {
            try
            {
                string rowId           = Request.QueryString["rowId"].ToString();
                string CRMUrl          = Request.QueryString["CRMUrl"].ToString();
                string PublisherName   = Request.QueryString["PublisherName"].ToString();
                string SocialMediaType = Request.QueryString["SocialMediaType"].ToString();
                var    auth            = new MvcAuthorizer
                {
                    CredentialStore = new SessionStateCredentialStore()
                };
                var oauth_consumer_key = Request.QueryString["oauth_consumer_key"];
                var oauth_token        = Request.QueryString["oauth_token"];
                auth.Parameters["oauth_consumer_key"] = oauth_consumer_key;
                auth.Parameters["oauth_token"]        = oauth_token;
                await auth.CompleteAuthorizeAsync(Request.Url);

                // await auth.CompleteAuthorizeAsync(new Uri( CurrentUrl[0]));

                //###########Start#############
                var credentials = auth.CredentialStore;
                credentials.ConsumerKey      = ConfigurationManager.AppSettings["consumerKey"];
                credentials.ConsumerSecret   = ConfigurationManager.AppSettings["consumerSecret"];
                credentials.OAuthToken       = auth.CredentialStore.OAuthToken;
                credentials.OAuthTokenSecret = auth.CredentialStore.OAuthTokenSecret;
                credentials.ScreenName       = auth.CredentialStore.ScreenName;
                credentials.UserID           = auth.CredentialStore.UserID;
                auth.CredentialStore         = credentials;
                var ctx = new TwitterContext(auth);

                User currentUser = (from user in ctx.User
                                    where user.Type == UserType.Show && user.ScreenName == auth.CredentialStore.ScreenName
                                    select user).ToList <User>().SingleOrDefault();

                //############END###############

                tbl_Twitter tbltwr = new tbl_Twitter();
                Guid        newId  = Guid.NewGuid();
                tbltwr.Id               = newId;
                tbltwr.Row_Id           = rowId;
                tbltwr.UserID           = auth.CredentialStore.UserID.ToString();
                tbltwr.OauthToken       = auth.CredentialStore.OAuthToken;
                tbltwr.OauthTokenSecret = auth.CredentialStore.OAuthTokenSecret;
                tbltwr.ScreenName       = auth.CredentialStore.ScreenName;
                tbltwr.Image_Url        = currentUser.ProfileImageUrlHttps;
                tbltwr.CRMUrl           = CRMUrl;
                tbltwr.PublisherName    = PublisherName;
                tbltwr.SocialMediaType  = SocialMediaType;

                tbltwr.AuthenticateDate = DateTime.Now;
                Obj.tbl_Twitter.Add(tbltwr);
                Obj.SaveChanges();

                return(RedirectToAction("Success", "Home"));
                //return RedirectToAction("BeginAsync", new { rowId = "Done" });
                //return Json(new { Id= newId.ToString(),IsSucces=true,Message="Success" }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return(Json(new { IsSucces = false, Message = ex.Message.ToString() }, JsonRequestBehavior.AllowGet));
            }
            //return RedirectToAction("Index", "Home");
        }