// GET api/LoggedUser
        public async Task<Doctor> Get(int tenantId)
        {
            credentials =
                await User.GetAppServiceIdentityAsync<AzureActiveDirectoryCredentials>(Request);

            string name = GetName();
            Doctor doctor = GetDoctor(tenantId, name);

            if (doctor != null)
            {
                doctor.Name = name;
                doctor.Picture = await GetPhoto(doctor);
            }

            return doctor;
        }
        public void PopulateProviderCredentials_AzureActiveDirectory_CreatesExpectedCredentials()
        {
            const string UserIdClaimValue = "AadId";

            AzureActiveDirectoryCredentials credentials = new AzureActiveDirectoryCredentials();

            TokenEntry tokenEntry = new TokenEntry("aad");
            tokenEntry.AccessToken = "TestAccessToken";
            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"),
                new ClaimSlim(TenantIdClaimType, "TestTenantId"),
                new ClaimSlim(ObjectIdentifierClaimType, "TestObjectId"),
            };
            tokenEntry.UserClaims = claims;
            tokenEntry.UserId = UserIdClaimValue;

            IPrincipalExtensions.PopulateProviderCredentials(tokenEntry, credentials);

            Assert.Equal("TestAccessToken", credentials.AccessToken);
            Assert.Equal("TestTenantId", credentials.Claims.GetValueOrDefault(TenantIdClaimType));
            Assert.Equal("TestObjectId", credentials.Claims.GetValueOrDefault(ObjectIdentifierClaimType));
            Assert.Equal(UserIdClaimValue, credentials.UserId);
            Assert.Equal(claims.Count, credentials.Claims.Count);
        }
        public void PopulateProviderCredentials_AzureActiveDirectory_CreatesExpectedCredentials()
        {
            const string UserIdClaimValue = "AadId";

            AzureActiveDirectoryCredentials credentials = new AzureActiveDirectoryCredentials();

            TokenEntry tokenEntry = new TokenEntry("aad");
            tokenEntry.AccessToken = "TestAccessToken";
            tokenEntry.IdToken = "TestIdToken";
            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"),
                new ClaimSlim(TenantIdClaimType, "TestTenantId"),
                new ClaimSlim(ObjectIdentifierClaimType, "TestObjectId"),
            };
            tokenEntry.UserClaims = claims;
            tokenEntry.UserId = UserIdClaimValue;

            IPrincipalExtensions.PopulateProviderCredentials(tokenEntry, credentials);

            // For AAD, the id_token value needs to get copied to AccessToken, not the access_token value.
            Assert.Equal("TestIdToken", credentials.AccessToken);
            Assert.Equal("TestTenantId", credentials.UserClaims.Single(c => string.Equals(c.Type, TenantIdClaimType, StringComparison.Ordinal)).Value);
            Assert.Equal("TestObjectId", credentials.UserClaims.Single(c => string.Equals(c.Type, ObjectIdentifierClaimType, StringComparison.Ordinal)).Value);
            Assert.Equal(UserIdClaimValue, credentials.UserId);
            Assert.Equal(claims.Count, credentials.UserClaims.Count());
        }
        public void PopulateProviderCredentials_AzureActiveDirectory_CreatesExpectedCredentials()
        {
            const string UserIdClaimValue = "AadId";

            AzureActiveDirectoryCredentials credentials = new AzureActiveDirectoryCredentials();

            TokenResult tokenResult = new TokenResult();
            tokenResult.Properties.Add(TokenResult.Authentication.AccessTokenName, "TestAccessToken");
            tokenResult.Properties.Add("TenantId", "TestTenantId");
            tokenResult.Properties.Add("ObjectId", "TestObjectId");
            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("TestTenantId", credentials.TenantId);
            Assert.Equal("TestObjectId", credentials.ObjectId);
            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;
            }
        }