Exemple #1
0
        public async Task IdentityServerProxy_GetUserInfoAsync_Valid_Token_Succeeds()
        {
            var clientConfiguration = new ClientConfiguration("MyClient", "MySecret");

            var client = new Client
            {
                ClientId      = clientConfiguration.Id,
                ClientSecrets = new List <Secret>
                {
                    new Secret(clientConfiguration.Secret.Sha256())
                },
                AllowedScopes = new[]
                {
                    "api1", IdentityServerConstants.StandardScopes.OfflineAccess,
                    IdentityServerConstants.StandardScopes.OpenId, IdentityServerConstants.StandardScopes.Profile
                },
                AllowedGrantTypes   = new[] { GrantType.ClientCredentials, GrantType.ResourceOwnerPassword },
                AccessTokenType     = AccessTokenType.Jwt,
                AccessTokenLifetime = 7200,
                AllowOfflineAccess  = true
            };

            var webHostBuilder = new IdentityServerTestWebHostBuilder()
                                 .AddClients(client)
                                 .AddApiResources(new ApiResource("api1", "api1name"))
                                 .AddApiScopes(new ApiScope("api1"))
                                 .AddIdentityResources(new IdentityResources.OpenId(), new IdentityResources.Profile())
                                 .UseResourceOwnerPasswordValidator(new SimpleResourceOwnerPasswordValidator())
                                 .UseProfileService(new SimpleProfileService())
                                 .CreateWebHostBuider();

            var identityServerProxy = new IdentityServerWebHostProxy(webHostBuilder);

            var scopes = new[] { "api1", "offline_access", "openid", "profile" };

            var tokenResponse = await identityServerProxy.GetResourceOwnerPasswordAccessTokenAsync(clientConfiguration,
                                                                                                   new UserLoginConfiguration("user", "password"),
                                                                                                   scopes);

            // We are breaking the pattern arrange / act / assert here but we need to make sure token requested successfully first
            Assert.False(tokenResponse.IsError, tokenResponse.Error ?? tokenResponse.ErrorDescription);


            var userInfoResponse = await identityServerProxy
                                   .GetUserInfoAsync(tokenResponse.AccessToken);

            Assert.NotNull(userInfoResponse);
            Assert.False(userInfoResponse.IsError);
            Assert.NotNull(userInfoResponse.Claims);

            var subjectClaim = userInfoResponse.Claims.First(claim => claim.Type == JwtClaimTypes.Subject);

            Assert.NotNull(subjectClaim);
            Assert.Equal("user", subjectClaim.Value);
        }