【问题标题】:The same Web API Code works on Windows 10 but not on Linux Debian Buster相同的 Web API 代码适用于 Windows 10,但不适用于 Linux Debian Buster
【发布时间】: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


    【解决方案1】:

    我已经使用您发布的控制器制作了一个示例项目,但我无法重现该行为。它正在工作,没有问题。

    尝试检查 Swagger 版本,应该在 5.6.3 以上

       <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
    

    【讨论】:

    • 是的。问题实际上不是 Swagger,我只是向大家展示了控制器是如何映射(或未映射)的。但是,如果我导航到路线本身,它也会返回 404,所有 em
    • 可能是和https有关?尝试像这里一样设置 https thegreenerman.medium.com/…
    猜你喜欢
    • 2022-10-01
    • 2022-11-21
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多