예제 #1
0
        void FillDataFromTwitter(TwitterCredentials credentials, out string first, out string last, out string profile)
        {
            first = credentials.UserClaims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value ?? string.Empty;
            profile =
                credentials.UserClaims.FirstOrDefault(c => c.Type == "urn:twitter:profile_image_url_https")?.Value ??
                string.Empty;

            //get largest image from twitter
            profile = profile.Replace("_normal", string.Empty);
            last = string.Empty;
        }
        public void PopulateProviderCredentials_Twitter_CreatesExpectedCredentials()
        {
            TwitterCredentials credentials = new TwitterCredentials();

            TokenResult tokenResult = new TokenResult();
            tokenResult.Properties.Add(TokenResult.Authentication.AccessTokenName, "TestAccessToken");
            tokenResult.Properties.Add("AccessTokenSecret", "TestAccessTokenSecret");
            Dictionary<string, string> claims = new Dictionary<string, string>
            {
                { "Claim1", "Value1" },
                { "Claim2", "Value1" },
                { "Claim3", "Value1" }
            };
            tokenResult.Claims = claims;

            MobileAppUser.PopulateProviderCredentials(tokenResult, credentials);

            Assert.Equal("TestAccessToken", credentials.AccessToken);
            Assert.Equal("TestAccessTokenSecret", credentials.AccessTokenSecret);
            Assert.Equal(claims.Count, credentials.Claims.Count);
        }
        public void PopulateProviderCredentials_Twitter_CreatesExpectedCredentials()
        {
            TwitterCredentials credentials = new TwitterCredentials();

            TokenEntry tokenEntry = new TokenEntry("twitter");
            tokenEntry.AccessToken = "TestAccessToken";
            tokenEntry.AccessTokenSecret = "TestAccessTokenSecret";
            List<ClaimSlim> claims = new List<ClaimSlim>
            {
                new ClaimSlim("Claim1", "Value1"),
                new ClaimSlim("Claim2", "Value2"),
                new ClaimSlim("Claim3", "Value3"),
            };
            tokenEntry.UserClaims = claims;

            IPrincipalExtensions.PopulateProviderCredentials(tokenEntry, credentials);

            Assert.Equal("TestAccessToken", credentials.AccessToken);
            Assert.Equal("TestAccessTokenSecret", credentials.AccessTokenSecret);
            Assert.Equal(claims.Count, credentials.Claims.Count);
        }
        internal static void PopulateProviderCredentials(TokenResult tokenResult, ProviderCredentials credentials)
        {
            if (tokenResult.Claims != null)
            {
                credentials.Claims = new Dictionary <string, string>(tokenResult.Claims);
            }

            FacebookCredentials facebookCredentials = credentials as FacebookCredentials;

            if (facebookCredentials != null)
            {
                facebookCredentials.AccessToken = tokenResult.Properties.GetValueOrDefault(TokenResult.Authentication.AccessTokenName);
                facebookCredentials.UserId      = tokenResult.Claims.GetValueOrDefault(ClaimTypes.NameIdentifier);
                return;
            }

            GoogleCredentials googleCredentials = credentials as GoogleCredentials;

            if (googleCredentials != null)
            {
                googleCredentials.AccessToken  = tokenResult.Properties.GetValueOrDefault(TokenResult.Authentication.AccessTokenName);
                googleCredentials.RefreshToken = tokenResult.Properties.GetValueOrDefault(TokenResult.Authentication.RefreshTokenName);
                googleCredentials.UserId       = tokenResult.Claims.GetValueOrDefault(ClaimTypes.NameIdentifier);

                string expiresOn = tokenResult.Properties.GetValueOrDefault("AccessTokenExpiration");
                if (!string.IsNullOrEmpty(expiresOn))
                {
                    googleCredentials.AccessTokenExpiration = DateTimeOffset.Parse(expiresOn, CultureInfo.InvariantCulture);
                }

                return;
            }

            AzureActiveDirectoryCredentials aadCredentials = credentials as AzureActiveDirectoryCredentials;

            if (aadCredentials != null)
            {
                aadCredentials.AccessToken = tokenResult.Properties.GetValueOrDefault(TokenResult.Authentication.AccessTokenName);
                aadCredentials.ObjectId    = tokenResult.Properties.GetValueOrDefault("ObjectId");
                aadCredentials.TenantId    = tokenResult.Properties.GetValueOrDefault("TenantId");
                aadCredentials.UserId      = tokenResult.Claims.GetValueOrDefault(ClaimTypes.NameIdentifier);
                return;
            }

            MicrosoftAccountCredentials microsoftAccountCredentials = credentials as MicrosoftAccountCredentials;

            if (microsoftAccountCredentials != null)
            {
                microsoftAccountCredentials.AccessToken  = tokenResult.Properties.GetValueOrDefault(TokenResult.Authentication.AccessTokenName);
                microsoftAccountCredentials.RefreshToken = tokenResult.Properties.GetValueOrDefault(TokenResult.Authentication.RefreshTokenName);
                microsoftAccountCredentials.UserId       = tokenResult.Claims.GetValueOrDefault(ClaimTypes.NameIdentifier);

                string expiresOn = tokenResult.Properties.GetValueOrDefault("AccessTokenExpiration");
                if (!string.IsNullOrEmpty(expiresOn))
                {
                    microsoftAccountCredentials.AccessTokenExpiration = DateTimeOffset.Parse(expiresOn, CultureInfo.InvariantCulture);
                }

                return;
            }

            TwitterCredentials twitterCredentials = credentials as TwitterCredentials;

            if (twitterCredentials != null)
            {
                twitterCredentials.AccessToken       = tokenResult.Properties.GetValueOrDefault(TokenResult.Authentication.AccessTokenName);
                twitterCredentials.AccessTokenSecret = tokenResult.Properties.GetValueOrDefault("AccessTokenSecret");
                twitterCredentials.UserId            = tokenResult.Claims.GetValueOrDefault(ClaimTypes.NameIdentifier);
                return;
            }
        }