Esempio n. 1
0
        public async Task <ActionResult> TwitterAuth()
        {
            var appClient = GetAppClient();

            var authRequestId = Guid.NewGuid().ToString();
            var redirectPath  = $"{Request.Scheme}://{Request.Host.Value}/Home/ValidateTwitterAuth";
            var redirectURL   = _myAuthRequestStore.AppendAuthenticationRequestIdToCallbackUrl(redirectPath, authRequestId);
            var authenticationRequestToken = await appClient.Auth.RequestAuthenticationUrlAsync(redirectURL);

            await _myAuthRequestStore.AddAuthenticationTokenAsync(authRequestId, authenticationRequestToken);

            return(new RedirectResult(authenticationRequestToken.AuthorizationURL));
        }
        internal async Task <string> GetAuthUrl()
        {
            var appClient = new TwitterClient(Environment.GetEnvironmentVariable("CONSUMER_KEY"), Environment.GetEnvironmentVariable("CONSUMER_SECRET"));
            var authenticationRequestId = Guid.NewGuid().ToString();
            var redirectPath            = "http://localhost:5001/validateTwitterAuth/";

            // Add the user identifier as a query parameters that will be received by `ValidateTwitterAuth`
            var redirectURL = _myAuthRequestStore.AppendAuthenticationRequestIdToCallbackUrl(redirectPath, authenticationRequestId);
            // Initialize the authentication process
            var authenticationRequestToken = await appClient.Auth.RequestAuthenticationUrlAsync(redirectURL);

            // Store the token information in the store
            await _myAuthRequestStore.AddAuthenticationTokenAsync(authenticationRequestId, authenticationRequestToken);

            // Return the authentication URL so we can display it to the user:
            return(authenticationRequestToken.AuthorizationURL);
        }
        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
            });
        }