How to use IdentityServer with docker-compose?
Answer a question Ciao, I'm developing one web application with these three components: IdentityServer: using IdentityServer4 Web API: using ASP.NET Core 5 Web App: using ASP.NET Blazor (server side).
Answer a question
Ciao, I'm developing one web application with these three components:
- IdentityServer: using IdentityServer4
- Web API: using ASP.NET Core 5
- Web App: using ASP.NET Blazor (server side).
I'm working in Windows with Docker for Desktop version 20.10.5, build 55c4c88.
I would debug and deploy my application's components as Docker containers. For each component I've added a Dockerfile and I've added to solution support for docker-compose.
Each Dockerfile expose the ports 80 and 443.
...
EXPOSE 80
EXPOSE 443
...
My docker-compose file is following:
version: '3.4'
services:
webapp:
image: ${DOCKER_REGISTRY-}webapp
ports:
- "44382:443"
build:
context: .
dockerfile: WebApp/Dockerfile
depends_on:
- identityserver
- webapi
networks:
- internal
webapi:
image: ${DOCKER_REGISTRY-}webapi
ports:
- "44305:443"
build:
context: .
dockerfile: WebApi/Dockerfile
depends_on:
- identityserver
networks:
- internal
identityserver:
image: ${DOCKER_REGISTRY-}identityserver
ports:
- "443:443"
build:
context: .
dockerfile: IdentityServer/Dockerfile
networks:
- internal
networks:
internal:
I've configured Web App with IdentityServer using these two packages:
<PackageReference Include="IdentityModel" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.2" />
And these configuration:
public void ConfigureServices(IServiceCollection services)
{
// Adding my dependencies...
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
// Temporarly I've disabled HTTPS but it doesn't let work the project
options.RequireHttpsMetadata = false;
options.Authority = Configuration["OpenID:Authority"];
options.ClientId = Configuration["OpenID:ClientId"];
options.ClientSecret = Configuration["OpenID:ClientSecret"];
options.ResponseType = "code";
options.Scope.Add("WebApi");
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.UsePkce = true;
options.Events = new OpenIdConnectEvents
{
OnAccessDenied = context =>
{
context.HandleResponse();
context.Response.Redirect("/");
return Task.CompletedTask;
}
};
});
services.AddMvcCore(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
}
I start the application by Visual Studio 2019 using docker-compose. When I try to start the application I obtain one error like following:
SocketException: Connection refused System.Net.Sockets.Socket+AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
HttpRequestException: Connection refused (identityserver:443) System.Net.Http.ConnectHelper.ConnectAsync(Func<SocketsHttpConnectionContext, CancellationToken, ValueTask> callback, DnsEndPoint endPoint, HttpRequestMessage requestMessage, CancellationToken cancellationToken)
IOException: IDX20804: Unable to retrieve document from: 'System.String'. Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(string address, CancellationToken cancel)
InvalidOperationException: IDX20803: Unable to obtain configuration from: 'System.String'. Microsoft.IdentityModel.Protocols.ConfigurationManager.GetConfigurationAsync(CancellationToken cancel)
**Note: I can access correctly to Home Page of IdentityServer accessing directly by url with its port.**
I'm sure that the client is correctly configured because previously to use containers authorization work correctly.
How can I resolve this problem?
Thank you a lot
Answers
Ciao,
my problem was not related about docker but was caused by HTTPS. I suspect using alias I've invalidate development HTTPS certificate generated by Visual Studio.
I've checked this adding these lines of codes for each component (identity server, web api and web app) and using HTTP protocol:
if (!env.IsDevelopment())
{
app.UseHttpsRedirection();
}
Navigating in HTTP and changing Identity Server configuration I can view login page. This is not a final solution but let me check that the problem was related about HTTPS certificate.
更多推荐
所有评论(0)