Exemple #1
0
        public async Task IdentityServerProxy_GetClientCredentialsAsync_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" },
                AllowedGrantTypes   = new[] { GrantType.ClientCredentials },
                AccessTokenType     = AccessTokenType.Jwt,
                AccessTokenLifetime = 7200
            };

            var webHostBuilder = new IdentityServerTestWebHostBuilder()
                                 .AddClients(client)
                                 .AddApiResources(new ApiResource("api1", "api1name"))
                                 .AddApiScopes(new ApiScope("api1"))
                                 .CreateWebHostBuider();

            var identityServerProxy = new IdentityServerWebHostProxy(webHostBuilder);

            var tokenResponse = await identityServerProxy.GetClientAccessTokenAsync(clientConfiguration, "api1");

            Assert.NotNull(tokenResponse);
            Assert.False(tokenResponse.IsError, tokenResponse.Error ?? tokenResponse.ErrorDescription);
            Assert.NotNull(tokenResponse.AccessToken);
            Assert.Equal(7200, tokenResponse.ExpiresIn);
            Assert.Equal("Bearer", tokenResponse.TokenType);
        }
        protected async Task <string> GetToken()
        {
            var tokenResponse = await _identityServerProxy
                                .GetClientAccessTokenAsync(
                _clientConf,
                "BookApi");

            return(tokenResponse.AccessToken);
        }
Exemple #3
0
        public async Task IdentityServerProxy_GetClientCredentialsAsync_Authorize_Api_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" },
                AllowedGrantTypes   = new[] { GrantType.ClientCredentials },
                AccessTokenType     = AccessTokenType.Jwt,
                AccessTokenLifetime = 7200
            };

            var webHostBuilder = new IdentityServerTestWebHostBuilder()
                                 .AddClients(client)
                                 .AddApiResources(new ApiResource("api1", "api1name"))
                                 .AddApiScopes(new ApiScope("api1"))
                                 .CreateWebHostBuider();

            var identityServerProxy = new IdentityServerWebHostProxy(webHostBuilder);

            var tokenResponse = await identityServerProxy.GetClientAccessTokenAsync(clientConfiguration, "api1");

            var apiWebHostBuilder = WebHost.CreateDefaultBuilder()
                                    .ConfigureServices(services =>
                                                       services.AddSingleton(identityServerProxy.IdentityServer.CreateHandler()))
                                    .UseStartup <Startup>();

            var apiServer = new TestServer(apiWebHostBuilder);

            var apiClient = apiServer.CreateClient();

            apiClient.SetBearerToken(tokenResponse.AccessToken);

            var apiResponse = await apiClient.GetAsync("api/auth");

            Assert.True(apiResponse.IsSuccessStatusCode, "should have been authenticated!");
        }