void FillDataFromMS(MicrosoftAccountCredentials credentials, out string first, out string last,
            out string profile)
        {
            first = credentials.UserClaims.FirstOrDefault(c => c.Type == ClaimTypes.GivenName)?.Value ?? string.Empty;
            last = credentials.UserClaims.FirstOrDefault(c => c.Type == ClaimTypes.Surname)?.Value ?? string.Empty;
            var id = credentials.UserClaims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value ??
                     string.Empty;

            profile = $"https://apis.live.net/v5.0/{id}/picture";
        }
        public void PopulateProviderCredentials_MicrosoftAccount_CreatesExpectedCredentials()
        {
            const string UserIdClaimValue = "MicrosoftId";

            MicrosoftAccountCredentials credentials = new MicrosoftAccountCredentials();

            TokenResult tokenResult = new TokenResult();
            tokenResult.Properties.Add(TokenResult.Authentication.AccessTokenName, "TestAccessToken");
            tokenResult.Properties.Add(TokenResult.Authentication.RefreshTokenName, "TestRefreshToken");
            tokenResult.Properties.Add("AccessTokenExpiration", "2015-03-12T16:49:28.504Z");
            Dictionary<string, string> claims = new Dictionary<string, string>
            {
                { "Claim1", "Value1" },
                { "Claim2", "Value1" },
                { "Claim3", "Value1" },
                { ClaimTypes.NameIdentifier, UserIdClaimValue }
            };
            tokenResult.Claims = claims;

            MobileAppUser.PopulateProviderCredentials(tokenResult, credentials);

            Assert.Equal("TestAccessToken", credentials.AccessToken);
            Assert.Equal("TestRefreshToken", credentials.RefreshToken);
            Assert.Equal(DateTimeOffset.Parse("2015-03-12T16:49:28.504Z"), credentials.AccessTokenExpiration);
            Assert.Equal(UserIdClaimValue, credentials.UserId);
            Assert.Equal(claims.Count, credentials.Claims.Count);
        }
        public void PopulateProviderCredentials_MicrosoftAccount_CreatesExpectedCredentials()
        {
            const string UserIdClaimValue = "MicrosoftId";

            MicrosoftAccountCredentials credentials = new MicrosoftAccountCredentials();

            TokenEntry tokenEntry = new TokenEntry("microsoft");
            tokenEntry.AccessToken = "TestAccessToken";
            tokenEntry.RefreshToken = "TestRefreshToken";
            tokenEntry.ExpiresOn = DateTime.Parse("2015-03-12T16:49:28.504Z");
            List<ClaimSlim> claims = new List<ClaimSlim>
            {
                new ClaimSlim("Claim1", "Value1"),
                new ClaimSlim("Claim2", "Value2"),
                new ClaimSlim("Claim3", "Value3"),
            };
            tokenEntry.UserClaims = claims;
            tokenEntry.UserId = UserIdClaimValue;

            IPrincipalExtensions.PopulateProviderCredentials(tokenEntry, credentials);

            Assert.Equal("TestAccessToken", credentials.AccessToken);
            Assert.Equal("TestRefreshToken", credentials.RefreshToken);
            Assert.Equal(DateTimeOffset.Parse("2015-03-12T16:49:28.504Z"), credentials.AccessTokenExpiration);
            Assert.Equal(UserIdClaimValue, credentials.UserId);
            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;
            }
        }