Nir*_*jan 1 c# swagger docker .net-core asp.net-core
net 核心 Web API 应用程序。我已经使用 Azure AD 身份验证创建了 swagger。当我使用 IIS 时,我的招摇正常工作。当我使用 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();
}
}
Run Code Online (Sandbox Code Playgroud)
下面是我的 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"]
Run Code Online (Sandbox Code Playgroud)
当我在 Docker 上运行时,http://localhost:54330/在浏览器中启动,如果我点击http://localhost:54330/swagger/index.html没有任何打开。如果我尝试点击http://localhost:44319/swagger/index.html那么我也无法打开 swagger。下面是我执行 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
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解决问题吗?任何帮助,将不胜感激。谢谢
默认情况下,swagger UI 只能在 dev env 中访问,因此您可以将 env 更改为 dev,或者更改应用程序 middelware 中的条件,以便将 swagger 中间件排除在该条件之外
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c =>
c.SwaggerEndpoint("/swagger/v1/swagger.json", "myappname v1"));
}
Run Code Online (Sandbox Code Playgroud)
所以它会变成这样
if (env.IsDevelopment()) {...}
app.UseSwagger();
app.UseSwaggerUI(c =>
c.SwaggerEndpoint("/swagger/v1/swagger.json","myappname v1"));
Run Code Online (Sandbox Code Playgroud)
使用 ASP.NET Core 的默认配置,swagger 仅适用于开发环境。将以下环境变量添加到您的 Dockerfile
FROM microsoft/dotnet:2.1-sdk AS build
ENV ASPNETCORE_URLS http://*:44319
ENV ASPNETCORE_ENVIRONMENT=Development #Add this line.
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"]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2897 次 |
| 最近记录: |