【发布时间】:2021-08-15 05:47:36
【问题描述】:
在下面的屏幕截图中,我的 Web API 在 Windows 10(左)上使用 .NET5.0 运行,在 Linux Debian Buster(右)(WSL2)上使用 .NET(核心?)5.0 运行相同的 Web API。
如您所见,在 Windows 上它可以正确映射控制器,但在 Linux 上却不行。我将 Swagger 页面显示得更加明确,但如果我手动访问路由,行为是相同的。
有谁知道可能是什么以及如何解决这个问题?我必须让它在 Linux 上运行,因为我的部署目标是基于 debian 的服务器。
以下是一些可能有用的代码(Startup.cs 和 Controller 声明)
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<Core.Contexts.ComiesContext>(o => {o.UseSqlServer("name=LocalComiesDBConn"); o.ConfigureWarnings(p => p. Ignore(30000));});
services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo { Title = "comies_services", Version = "v1" }));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<Core.Contexts.ComiesContext>()
.AddDefaultTokenProviders();
var signingConfigurations = new Structures.ModelsConfigurations.SigningConfigurations(Configuration["TokenConfigurations:securityKey"]);
services.AddSingleton(signingConfigurations);
var tokenConfigurations = new AuthenticationConfiguration();
new ConfigureFromConfigurationOptions<AuthenticationConfiguration>(Configuration.GetSection("TokenConfigurations")).Configure(tokenConfigurations);
services.AddSingleton(tokenConfigurations);
services.AddAuthentication(authOptions =>
{
authOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
authOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(jwtBearerOptions =>
{
var paramsValidation = jwtBearerOptions.TokenValidationParameters;
paramsValidation.IssuerSigningKey = signingConfigurations.Key;
paramsValidation.ValidAudience = tokenConfigurations.Audience;
paramsValidation.ValidIssuer = tokenConfigurations.Issuer;
paramsValidation.ValidateIssuerSigningKey = true;
paramsValidation.ValidateLifetime = true;
paramsValidation.ClockSkew = TimeSpan.Zero;
});
// Ativa o uso do token como forma de autorizar o acesso
// a recursos deste projeto
services.AddAuthorization(auth =>
{
auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser().Build());
});
services.AddScoped<AuthenticatedOperator>();
services.AddScoped<AuthenticationService>();
services.AddTransient<UserManager<ApplicationUser>>();
services.AddTransient<SignInManager<ApplicationUser>>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "comies_services v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
AuthController.cs
[Route("api/v1/auth")]
[ApiController]
public class AuthenticationController : ControllerBase
{
private readonly ComiesContext _context;
private readonly AuthenticationService _authenticationService;
public AuthenticationController(ComiesContext context, AuthenticationService authenticationService)
{
_context = context; _authenticationService = authenticationService;
}
// GET: api/<AuthenticationController>
[HttpGet("register/template")]
public async Task<ActionResult<Store>> Get()
{
///hidden implementation, but trust me, this endpoint is working (when it's mapped)
}
【问题讨论】:
标签: c# linux windows .net-core api-design