internal async Task <TwitterClient> StartListener()
        {
            using (var listener = new HttpListener())
            {
                listener.Prefixes.Add("http://localhost:5001/validateTwitterAuth/");

                listener.Start();

                for (; ;)
                {
                    HttpListenerContext context = listener.GetContext();
                    HttpListenerRequest request = context.Request;

                    using (HttpListenerResponse response = context.Response)
                    {
                        var appClient = new TwitterClient(Environment.GetEnvironmentVariable("CONSUMER_KEY"), Environment.GetEnvironmentVariable("CONSUMER_SECRET"));

                        // Extract the information from the redirection url
                        var requestParameters = await RequestCredentialsParameters.FromCallbackUrlAsync(context.Request.Url.Query, _myAuthRequestStore);

                        // Request Twitter to generate the credentials.
                        var userCreds = await appClient.Auth.RequestCredentialsAsync(requestParameters);

                        // Congratulations the user is now authenticated!
                        var userClient = new TwitterClient(userCreds);

                        return(userClient);
                    }
                }
            }
        }
Beispiel #2
0
        public async Task <RedirectResult> OnGet()
        {
            var appClient = new TwitterClient(TwitifyController.ConsumerKey, TwitifyController.ConsumerSecret);

            // Extract the information from the redirection url
            var requestParameters = await RequestCredentialsParameters.FromCallbackUrlAsync(Request.QueryString.Value, TwitifyController._myAuthRequestStore);

            // Request Twitter to generate the credentials.
            var userCreds = await appClient.Auth.RequestCredentialsAsync(requestParameters);

            Response.Cookies.Append("AccessToken", userCreds.AccessToken);
            Response.Cookies.Append("AccessTokenSecret", userCreds.AccessTokenSecret);
            // Congratulations the user is now authenticated!
            var userClient = new TwitterClient(userCreds);
            var user       = await userClient.Users.GetAuthenticatedUserAsync();

            Response.Cookies.Append("Bio", userClient.Users.GetAuthenticatedUserAsync().Result.Description);
            Response.Cookies.Append("UserName", userClient.Users.GetAuthenticatedUserAsync().Result.ScreenName);
            Response.Cookies.Append("Profile", userClient.Users.GetAuthenticatedUserAsync().Result.ProfileImageUrl400x400);
            Uri baseUri = TwitifyController.SpotifyUri;

            var loginRequest = new LoginRequest(baseUri, TwitifyController.clientId, LoginRequest.ResponseType.Token)
            {
                Scope = new[] { Scopes.UserReadPlaybackState, Scopes.UserReadEmail, Scopes.UserReadCurrentlyPlaying }
            };

            return(Redirect(loginRequest.ToUri().ToString()));
        }
Beispiel #3
0
        public async Task <ActionResult> ValidateTwitterAuth()
        {
            var appClient = GetAppClient();

            // RequestCredentialsParameters.FromCallbackUrl does 3 things:
            // * Extract the id from the callback
            // * Get the AuthenticationRequest from the store
            // * Remove the request from the store as it will no longer need it
            // This logic can be implemented manually if you wish change the behaviour
            var requestParameters = await RequestCredentialsParameters.FromCallbackUrlAsync(Request.QueryString.Value, _myAuthRequestStore);

            var userCreds = await appClient.Auth.RequestCredentialsAsync(requestParameters);

            var userClient = new TwitterClient(userCreds);
            var user       = await userClient.Users.GetAuthenticatedUserAsync();

            ViewBag.User = user;

            return(View());
        }
        public override LoginProfile ProcessAuthoriztion(HttpContext context, IDictionary <string, string> @params)
        {
            if (!string.IsNullOrEmpty(context.Request["denied"]))
            {
                return(LoginProfile.FromError(new Exception("Canceled at provider")));
            }

            var appClient = new TwitterClient(TwitterKey, TwitterSecret);

            if (string.IsNullOrEmpty(context.Request["oauth_token"]))
            {
                var callbackAddress = new UriBuilder(RedirectUri)
                {
                    Query = "state=" + HttpUtility.UrlEncode(context.Request.GetUrlRewriter().AbsoluteUri)
                };

                var authenticationRequestId = Guid.NewGuid().ToString();

                // Add the user identifier as a query parameters that will be received by `ValidateTwitterAuth`
                var redirectURL = _myAuthRequestStore.AppendAuthenticationRequestIdToCallbackUrl(callbackAddress.ToString(), authenticationRequestId);

                // Initialize the authentication process
                var authenticationRequestToken = appClient.Auth.RequestAuthenticationUrlAsync(redirectURL)
                                                 .ConfigureAwait(false)
                                                 .GetAwaiter()
                                                 .GetResult();

                // Store the token information in the store
                _myAuthRequestStore.AddAuthenticationTokenAsync(authenticationRequestId, authenticationRequestToken)
                .ConfigureAwait(false)
                .GetAwaiter()
                .GetResult();

                context.Response.Redirect(authenticationRequestToken.AuthorizationURL, true);

                return(null);
            }

            // Extract the information from the redirection url
            var requestParameters = RequestCredentialsParameters.FromCallbackUrlAsync(context.Request.RawUrl, _myAuthRequestStore).GetAwaiter().GetResult();
            // Request Twitter to generate the credentials.
            var userCreds = appClient.Auth.RequestCredentialsAsync(requestParameters)
                            .ConfigureAwait(false)
                            .GetAwaiter()
                            .GetResult();

            // Congratulations the user is now authenticated!
            var userClient = new TwitterClient(userCreds);

            var user = userClient.Users.GetAuthenticatedUserAsync()
                       .ConfigureAwait(false)
                       .GetAwaiter()
                       .GetResult();

            var userSettings = userClient.AccountSettings.GetAccountSettingsAsync()
                               .ConfigureAwait(false)
                               .GetAwaiter()
                               .GetResult();

            return(user == null
                       ? null
                       : new LoginProfile
            {
                Name = user.Name,
                DisplayName = user.ScreenName,
                Avatar = user.ProfileImageUrl,
                Locale = userSettings.Language.ToString(),
                Id = user.Id.ToString(CultureInfo.InvariantCulture),
                Provider = ProviderConstants.Twitter
            });
        }