Ejemplo n.º 1
0
        protected async void PostUpdateButton_Click(object sender, EventArgs e)
        {
            var auth = new AspNetAuthorizer
            {
                CredentialStore = new SessionStateCredentialStore(),
                GoToTwitterAuthorization = twitterUrl => { }
            };

            var ctx = new TwitterContext(auth);

            await ctx.TweetAsync(UpdateTextBox.Text);

            SuccessLabel.Visible = true;
        }
        protected async void RefreshButton_Click(object sender, EventArgs e)
        {
            var auth = new AspNetAuthorizer
            {
                CredentialStore = new SessionStateCredentialStore(),
                GoToTwitterAuthorization = twitterUrl => { }
            };

            var ctx = new TwitterContext(auth);

            var tweets =
                await
                (from tweet in ctx.Status
                 where tweet.Type == StatusType.Home
                 select tweet)
                .ToListAsync();

            TwitterListView.DataSource = tweets;
            TwitterListView.DataBind();
        }
Ejemplo n.º 3
0
        protected async void Page_Load(object sender, EventArgs e)
        {
            auth = new AspNetAuthorizer
            {
                CredentialStore = new SessionStateCredentialStore
                {
                    ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],
                    ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"]
                },
                GoToTwitterAuthorization =
                    twitterUrl => Response.Redirect(twitterUrl, false)
            };

            if (!Page.IsPostBack && Request.QueryString["oauth_token"] != null)
            {
                await auth.CompleteAuthorizeAsync(Request.Url);

                // 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;
                //

                Response.Redirect("~/Default.aspx", false);
            }
        }
Ejemplo n.º 4
0
        async Task StoreTwitterCredentials(ApplicationUser user)
        {
            var claimsIdentity = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
            if (claimsIdentity == null) return;

            IList<Claim> currentClaims = await UserManager.GetClaimsAsync(user.Id);
            Claim accessToken = claimsIdentity.FindFirst(LinqToTwitterAuthenticationProvider.AccessToken);
            Claim accessTokenSecret = claimsIdentity.FindFirst(LinqToTwitterAuthenticationProvider.AccessTokenSecret);

            var claimKeys =
                (from claim in currentClaims
                 select claim.Type)
                .ToList();

            if (!claimKeys.Contains(accessToken.Type))
                await UserManager.AddClaimAsync(user.Id, accessToken);

            if (!claimKeys.Contains(accessTokenSecret.Type))
                await UserManager.AddClaimAsync(user.Id, accessTokenSecret);

            var auth = new AspNetAuthorizer
            {
                CredentialStore = new SessionStateCredentialStore
                {
                    ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],
                    ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"],
                    OAuthToken = accessToken.Value,
                    OAuthTokenSecret = accessTokenSecret.Value
                }
            };

            using (var ctx = new TwitterContext(auth))
            {
                var twitterUser =
                    await
                    (from acct in ctx.Account
                     where acct.Type == AccountType.VerifyCredentials
                     select acct.User)
                    .SingleOrDefaultAsync();

                string name = twitterUser.Name;
                // you can do something with Twitter User data here too
            }
        }