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 IdentityServerHostBuilder() .AddClients(client) .AddApiResources(new ApiResource("api1", "api1name")) .CreateWebHostBuider(); var identityServerProxy = new IdentityServerProxy(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); }
public async Task Test_Not_Working() { 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, AllowOfflineAccess = true }; var webHostBuilder = new IdentityServerWebHostBuilder() .AddClients(client) .AddApiResources(new ApiResource("api1", "api1name")) .CreateWebHostBuilder(); var identityServerProxy = new IdentityServerProxy(webHostBuilder); var tokenResponse = await identityServerProxy.GetClientAccessTokenAsync(clientConfiguration, "api1"); var apiServer = new TestServer(new WebHostBuilder() .ConfigureAppConfiguration(builder => { var configuration = new ConfigurationBuilder() .AddJsonFile(Path.Combine(AppContext.BaseDirectory, "appsettings.json")) .Build(); builder.AddConfiguration(configuration); }) .ConfigureServices( services => services.AddSingleton(identityServerProxy.IdentityServer.CreateHandler())) .UseStartup <Startup>()); var apiClient = apiServer.CreateClient(); apiClient.SetBearerToken(tokenResponse.AccessToken); var response = await apiClient.GetAsync("/api/values/"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
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 IdentityServerHostBuilder() .AddClients(client) .AddApiResources(new ApiResource("api1", "api1name")) .CreateWebHostBuider(); var identityServerProxy = new IdentityServerProxy(webHostBuilder); var tokenResponse = await identityServerProxy.GetClientAccessTokenAsync(clientConfiguration, "api1"); var apiWebHostBuilder = WebHost.CreateDefaultBuilder() .ConfigureServices(services => services.AddSingleton(identityServerProxy.IdentityServer.CreateHandler())) .UseStartup <IdentityServer4.Api.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!"); }