【发布时间】:2019-10-23 08:19:31
【问题描述】:
net core Web API 应用程序。我用 Azure AD 身份验证创建了招摇。当我使用 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();
}
}
下面是我的 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,那么我也无法打开招摇。下面是我做 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
有人可以帮我解决这个问题吗?任何帮助,将不胜感激。谢谢
【问题讨论】:
-
我可以确认 docker 中的招摇工作。我猜在 docker 中它会因为 UseHsts 而尝试使用 https。
-
糟糕,UseHsts 似乎不像我想的那样工作,没关系。
-
好像dockerfile不对,你把项目编译到/app目录下,但是WORKDIR还在/src/LocationServicesAPI/,所以没有dll可以运行。
-
你还需要暴露端口 80 ,所以在 dockerfile 中添加 EXPOSE 80 行
-
另外我希望它在 Kestrel 中运行,如果不是,那么您需要将它托管在 docker 中的 Kestrel 中。
标签: c# docker asp.net-core .net-core swagger