簡體   English   中英

ASP.NET Core 3.0 Web API 路由不起作用

[英]ASP.NET Core 3.0 Web API routing not working

我正在嘗試訪問我的 dotnet core 3.1 解決方案中的 jwt 控制器。

這是我的 jwt 控制器:

[Authorize]
[ApiController]
[Route("api/[controller]")]
public class JwtController : ControllerBase 
{
        #region Variables
        private readonly IUserAuthorisationServices _tokenService;
        private readonly IOptions<JwtTokenOptions> jwtOptions;
        private readonly ILogger logger;
        private readonly JsonSerializerSettings _serializerSettings;
        #endregion

        public JwtController(IUserAuthorisationServices tokenService,
                             IOptions<JwtTokenOptions> jwtOptions,
                             ILoggerFactory loggerFactory) 
        {
            if (loggerFactory is null) throw new ArgumentNullException(nameof(loggerFactory));

            _tokenService = tokenService ?? throw new ArgumentNullException(nameof(tokenService));

            jwtOptions = jwtOptions ?? throw new ArgumentNullException(nameof(jwtOptions));
            logger = loggerFactory.CreateLogger<JwtController>();
            _serializerSettings = new JsonSerializerSettings {
                Formatting = Formatting.Indented
            };
            //loggingRepository = _errorRepository;
            //ThrowIfInvalidOptions(this.jwtOptions);
        }

  [AllowAnonymous]
  [Route("authenticate")]
  [HttpPost]
  public async Task<IActionResult> Authenticate([FromBody] LoginViewModel model)
  {
            var userContext = _tokenService.Authenticate(model.Email, model.Password);

            if (userContext.Principal == null) {
                logger.LogInformation($"Invalid username ({model.Email}) or password ({model.Password})");
                return BadRequest(new { message = "Username or password is incorrect" });
            }

            return Ok(await _tokenService.CreateTokenAsync(userContext).ConfigureAwait(false));
  }

   [Route("JWTStatus")]
   [HttpGet]
   public static IActionResult GetJwtStatus() 
   {
            // It made it here so it was authenticated.
            return new OkResult();
   }
}

我已經逐步完成了這個過程,它到達了這個中間件,然后找不到控制器立即返回,同時向瀏覽器提供 404。

這是我用來訪問此控制器操作的內容:

login(email: string, password: string) {
    this.userLogin.Email = email;
    this.userLogin.Password = password;

    // Do the fetch!
    const t = fetch("/api/authenticate",
            {
                method: "POST",
                body: JSON.stringify(this.userLogin),
                headers: new Headers({ "content-type": "application/json" })
            })
        .then(response => {

我注意到提取正在使用“api/authenticate”,當我查看 HttpRequest 時,它得到了相同的 url,但它找不到它。

這是它在返回之前到達的中間件。

namespace JobsLedger.AUTHORISATION.API.SessionMiddleware
{
    public class ConfigureSessionMiddleware
    {
        private readonly RequestDelegate _next;

        public ConfigureSessionMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task InvokeAsync(HttpContext httpContext, IUserSession userSession, ISessionServices sessionServices)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            if (userSession == null)
            {
                throw new ArgumentNullException(nameof(userSession));
            }

            if (sessionServices == null)
            {
                throw new ArgumentNullException(nameof(sessionServices));
            }

            if (httpContext.User.Identities.Any(id => id.IsAuthenticated))
            {
                if (httpContext.Session.GetString("connectionString") == null) // Session needs to be set..
                {
                    userSession.UserId = httpContext.User.Claims.FirstOrDefault(x => x.Type == "userId")?.Value;
                    userSession.ConnectionString = sessionServices.ConnectionStringFromUserId(userSession.UserId);
                    httpContext.Session.SetString("userId", userSession.UserId);
                    httpContext.Session.SetString("connectionString", userSession.ConnectionString);
                }
                else  //  Session set so all we need to is to build userSession for data access..
                {
                    userSession.UserId = httpContext.Session.GetString("userId");
                    userSession.ConnectionString = httpContext.Session.GetString("connectionString");
                }
            }

            // Call the next delegate/middleware in the pipeline
            await _next.Invoke(httpContext).ConfigureAwait(false);
        }
    }
}

由於尚未設置用戶身份,因此假設它會直接跳過此設置。 所以它真的直接到:

await _next.Invoke(httpContext).ConfigureAwait(false);

然后進入控制器動作..但它直接以 404 結束。

在輸出窗口中,我得到:

Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request starting HTTP/2.0 POST https://localhost:44301/api/authenticate text/plain;charset=UTF-8 76
Microsoft.AspNetCore.Cors.Infrastructure.CorsService: Information: CORS policy execution successful.
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished in 17.1363ms 404 

你可以看到 404。

怎么找不到動作?

是否可能是因為它是 https 和 http 2.0?

為了完整起見,這里是該中間件處的 httpContext:

在此處輸入圖片說明

更新...

我在啟動時既不使用 MVC 也不只是端點......

也許它是 Startup.cs 中的管道 .. 這是我的 startup.cs

[assembly: NeutralResourcesLanguage("en")]

namespace JobsLedger.API {
    public class Startup {
        //private const string SecretKey = "needtogetthisfromenvironment";

        //private readonly SymmetricSecurityKey
        //  _signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(SecretKey));
        private readonly IWebHostEnvironment _env;
        private readonly IConfiguration _configuration;  // { get; }

        public Startup(IWebHostEnvironment env, IConfiguration configuration) {
            _env = env;
            _configuration = configuration;
        }


        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services) {
            // Add framework services.
            services.AddCors();
            services.AddControllers();
            services.AddOptions();

            services.AddDbContext<CATALOGContext>(options => options.UseSqlServer(_configuration.GetConnectionString("CatalogConnection"), b => b.MigrationsAssembly("JobsLedger.CATALOG")));
            services.AddDbContext<DATAContext>(options => options.UseSqlServer(_configuration.GetConnectionString("TenantDbConnection"), a => a.MigrationsAssembly("JobsLedger.DATA")));


            // Make authentication compulsory across the board (i.e. shut
            // down EVERYTHING unless explicitly opened up).
            // Use policy auth.
            services.AddAuthorization(options => {
                options.AddPolicy("TenantAdmin", policy => policy.RequireClaim(ClaimTypes.Role, "TenantAdmin"));
                options.AddPolicy("Admin", policy => policy.RequireClaim(ClaimTypes.Role, "Admin"));
                options.AddPolicy("Employee", policy => policy.RequireClaim(ClaimTypes.Role, "Employee"));
            });

            //services.ConfigureApplicationInjection();

            // Get jwt options from app settings
            var tokenOptions = _configuration.GetSection("Authentication").Get<JwtTokenOptions>();

            services.AddAuthentication(x => {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(options => {
                options.TokenValidationParameters = new TokenValidationParameters {
                    ValidateIssuer = true,
                    ValidateAudience = true,
                    ValidateLifetime = true,
                    ValidateIssuerSigningKey = true,
                    RequireExpirationTime = true,
                    ValidIssuer = tokenOptions.Issuer,
                    ValidAudience = tokenOptions.Audience,
                    IssuerSigningKey = new SymmetricSecurityKey(
                        Encoding.UTF8.GetBytes(tokenOptions.SigningKey)),
                    ClockSkew = TimeSpan.Zero
                };
            });

            services
                .AddDistributedMemoryCache()
                .AddSession()


                // Repositories - DATA
                .AddScoped<IClientDATARepository, ClientDATARepository>()
                .AddScoped<ILoggingDATARepository, LoggingDATARepository>()
                .AddScoped<IJobDATARepository, JobDATARepository>()
                .AddScoped<IBrandDATARepository, BrandDATARepository>()
                .AddScoped<ITypeDATARepository, TypeDATARepository>()
                .AddScoped<IStateDATARepository, StateDATARepository>()
                .AddScoped<IStatusDATARepository, StatusDATARepository>()
                .AddScoped<ISuburbDATARepository, SuburbDATARepository>()
                .AddScoped<ICounterDATARepository, CounterDATARepository>()

                // Repositories - CATALOG
                .AddScoped<ITenantCATALOGRepository, TenantCATALOGRepository>()
                .AddScoped<IUserCATALOGRepository, UserCATALOGRepository>()
                .AddScoped<IRoleCATALOGRepository, RoleCATALOGRepository>()
                .AddScoped<ICounterCATALOGRepository, CounterCATALOGRepository>()
                .AddScoped<ISuburbCATALOGRepository, SuburbCATALOGRepository>()
                .AddScoped<IStateCATALOGRepository, StateCATALOGRepository>()

                // Business services
                // Services - API
                .AddScoped<IClientServices, ClientServices>()
                .AddScoped<IJobServices, JobServices>()

                //Services - Catalog
                .AddScoped<ITenantServices, TenantServices>()
                .AddScoped<IUserServices, UserServices>()
                //.AddScoped<IUserValidateService, UserValidateService>()

                // Services - Shared
                .AddScoped<IAddressDropdownServices, AddressDropdownServices>()

                //Services - Auth
                .AddScoped<ICryptoService, CryptoService>()
                .AddScoped<IUserAuthorisationServices, UserAuthorisationServices>()
                .AddScoped<IUserSession, UserSession>()
                .AddScoped<ISessionServices, SessionServices>()

                // CATALOG services - Initialisations.
                .AddScoped<ICATALOGCounterInitialiser, CATALOGCounterInitialiser>()
                .AddScoped<ICATALOGStateInitialiser, CATALOGStateInitialiser>()
                .AddScoped<ICATALOGSuburbInitialiser, CATALOGSuburbInitialiser>()
                .AddScoped<IRoleInitialiser, CATALOGRoleInitialiser>()
                .AddScoped<ICATALOGTenantAndUserInitialisations, CATALOGTenantAndUserInitialiser>()

                // DATA Services - Initialisations.
                .AddScoped<IBrandInitialiser, BrandInitialiser>()
                .AddScoped<IDATACounterInitialiser, DATACounterInitialiser>()
                .AddScoped<IDATAStateInitialiser, DATAStateInitialiser>()
                .AddScoped<IDATASuburbInitialiser, DATASuburbInitialiser>()
                .AddScoped<IStatusInitialiser, StatusInitialiser>()
                .AddScoped<ITypeInitialiser, TypeInitialiser>()
                .AddScoped<IVisitTypeInitialiser, VisitTypeInitialiser>()

                // TESTDATA Services - Initialisations.
                .AddScoped<ITESTDATAInitialisations, TESTDATAInitialisations>()
                .AddScoped<ITESTDATATenantAndUserInitialisations, TESTDATATenantAndUserInitialisations>()

                .AddTransient<IDATAContextFactory, DATAContextFactory>()
                .AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); // For getting the user.
        }

        public static void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
            if (env.IsDevelopment()) {
                app.UseDeveloperExceptionPage();
            }
            else {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseExceptionHandler(options => {
                options.Run(async context => {
                    var ex = context.Features.Get<IExceptionHandlerPathFeature>();
                    if (ex?.Error != null) {
                        Debugger.Break();
                    }
                });
            });

            app.UseRouting();

            // global cors policy
            app.UseCors(x => x
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader());

            app.UseAuthentication();
            app.UseHttpsRedirection();
            app.UseFileServer();
            app.UseStaticFiles();
            app.UseSession();
            app.UseAuthorization();

            app.UseConfigureSession();

            app.EnsureCATALOGMigrationAndInitialise();
            app.EnsureDATAMigrationsAndInitialise();

            app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
        }
    }
}

好的! 知道了。 根據您當前的路由配置,您的路由端點是:

/authenticate // <-- not api/authenticate
/JWTStatus    // <-- not api/JWTStatus

因為您正在覆蓋控制器級別的路由。

如果您希望api/authenticateapi/JWTStatus作為您的路由端點,那么您的操作級別路由應如下所示:

[Authorize]
[ApiController]
//Remove the controller level route from here
public class JwtController : ControllerBase 
{
    [AllowAnonymous]
    [Route("api/authenticate")] // <-- Here it is
    [HttpPost]
    public async Task<IActionResult> Authenticate([FromBody] LoginViewModel model)
    {
       .........
    }

    [Route("api/JWTStatus")] // <-- Here it is
    [HttpGet]
    public IActionResult GetJwtStatus() 
    {
       .......
    }
}

但我的建議是遵循以下約定:

[Authorize]
[ApiController]
[Route("api/[controller]/[action]")] // <-- Here it is
public class JwtController : ControllerBase 
{
     [AllowAnonymous]
     [HttpPost]
     public async Task<IActionResult> Authenticate([FromBody] LoginViewModel model)
     {
        .......  
     }


     [HttpGet]
     public IActionResult GetJwtStatus() 
     {
        ......
     }
}

現在不再需要操作級路由,您的端點將是:

api/Jwt/Authenticate
api/Jwt/GetJwtStatus

您的控制器級別路由屬性正在產生問題。
控制器和操作級別的路由屬性都需要更改。

首先,刪除控制器的路由屬性

然后,修改動作級路由屬性如下

[Route("api/authenticate")]


[Route("api/JWTStatus")]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM