net核心的Web API应用程序。我创建了Swagger with Azure AD身份验证。当我使用IIS时,我的swagger可以正常工作。当我使用Docker运行时,我得到了这个网站无法访问。下面是我的启动代码。
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
azureActiveDirectoryOptions = configuration.GetSection("AzureAd").Get<AzureActiveDirectoryOptions>();
swaggerUIOptions = configuration.GetSection("Swagger").Get<SwaggerUIOptions>();
}
public IConfiguration Configuration { get; }
private readonly AzureActiveDirectoryOptions azureActiveDirectoryOptions;
private readonly SwaggerUIOptions swaggerUIOptions;
//
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services
.AddAuthentication(o =>
{
o.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(o =>
{
o.Authority = azureActiveDirectoryOptions.Authority;
o.TokenValidationParameters = new TokenValidationParameters
{
ValidAudiences = new List<string>
{
azureActiveDirectoryOptions.AppIdUri,
azureActiveDirectoryOptions.ClientId
},
ValidateIssuer = true,
ValidateAudience = true,
ValidIssuer = "https://KmartAus.onmicrosoft.com/oauth2/default",
RoleClaimType = ClaimTypes.Role
};
});
services.AddMvc(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1); ;
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
c.AddSecurityDefinition("oauth2", new OAuth2Scheme
{
Type = "oauth2",
Flow = "implicit",
AuthorizationUrl = swaggerUIOptions.AuthorizationUrl,
TokenUrl = swaggerUIOptions.TokenUrl,
Scopes = new Dictionary<string, string>
{
{"Read", "13269k8-a2ea-45a1-96e7-6580f57b6e30/.default" }
}
});
c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>
{
{ "oauth2", new[] { "readAccess", "writeAccess" } }
});
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.OAuthClientId(swaggerUIOptions.ClientId);
c.OAuthClientSecret(swaggerUIOptions.ClientSecret);
c.OAuthRealm(azureActiveDirectoryOptions.ClientId);
c.OAuthAppName("Swagger");
//c.OAuthAdditionalQueryStringParams(new { resource = azureActiveDirectoryOptions.ClientId });
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
app.UseAuthentication();
app.UseMvc();
}
}
下面是我的docker文件。
FROM microsoft/dotnet:2.1-sdk AS build
ENV ASPNETCORE_URLS http://*:44319
EXPOSE 44319
WORKDIR /src
COPY ["LocationServicesAPI/LocationServicesAPI.csproj", "LocationServicesAPI/"]
RUN dotnet restore "LocationServicesAPI/LocationServicesAPI.csproj"
COPY . .
WORKDIR /src/LocationServicesAPI/
RUN dotnet build LocationServicesAPI.csproj -c Release -o /app
ENTRYPOINT ["dotnet", "LocationServicesAPI.dll"]
当我在Docker上点击运行时,http://localhost:54330/在浏览器中启动,如果我点击http://localhost:54330/swagger/index.html,什么都不会打开。如果我试图击中http://localhost:44319/swagger/index.html,那么我也无法打开 Swagger 。下面是我的集装箱港口Map时,我做 Docker ps。
44319/tcp,0.0.0.0:54330->80/tcp以下文件存在于容器中。
Controllers Dockerfile LocationServicesAPI.csproj LocationServicesAPI.csproj.user Models Program.cs Properties Startup.cs appsettings.Development.json appsettings.json bin obj out wwwroot
有人能帮我解决这个问题吗?如果你能帮忙的话,我将不胜感激。谢谢
3条答案
按热度按时间klsxnrf11#
使用ASP.NETCore的默认配置,swagger只在开发环境中工作。将以下环境变量添加到Dockerfile中
beq87vna2#
默认情况下,swagger UI只能在dev env中访问,因此您可以将env更改为dev,或者更改app middelware中的条件,以便将swagger中间件从条件中删除
所以会变成这样
gg0vcinb3#
我今天也遇到了同样的问题,终于解决了。您可以使用以下文件作为示例。
实际上,为我解决这个问题的是添加这个:
以下是整个dockerfile:
我希望它能达到目的。