下面是我编写了一个中间件的代码片段,该中间件接收api请求并验证它并发送响应。
但是当请求在那时有效时它不会调用API,它会显示404 Not Found Error。
中间件代码段
// You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
public class ApiKeyValidatorsMiddleware
{
private readonly RequestDelegate _next;
IValidateRequest _IValidateRequest { get; set; }
public ApiKeyValidatorsMiddleware(RequestDelegate next , IValidateRequest ivalidaterequest)
{
_next = next;
_IValidateRequest = ivalidaterequest;
}
public async Task Invoke(HttpContext httpContext)
{
try
{
var queryString = httpContext.Request.Query;
StringValues keyvalue;
queryString.TryGetValue("key", out keyvalue);
if (httpContext.Request.Method != "POST")
{
httpContext.Response.StatusCode = 405; //Method Not Allowed
await httpContext.Response.WriteAsync("Method Not Allowed");
return;
}
if (keyvalue.Count == 0)
{
httpContext.Response.StatusCode = 400; //Bad Request
await httpContext.Response.WriteAsync("API Key is missing");
return;
}
else
{
var serviceName = httpContext.Request.Path.Value.Replace(@"/", string.Empty);
if (!_IValidateRequest.ValidateKeys(keyvalue) && !_IValidateRequest.IsValidServiceRequest(keyvalue, serviceName))
{
httpContext.Response.StatusCode = 401; //UnAuthorized
await httpContext.Response.WriteAsync("Invalid User Key");
return;
}
}
await _next.Invoke(httpContext);
}
catch (Exception)
{
throw;
}
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class MiddlewareExtensions
{
public static IApplicationBuilder UseMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<ApiKeyValidatorsMiddleware>();
}
}
启动
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseSession();
//Middleware Call
app.Map("/api", appBuilder => appBuilder.UseMiddleware<ApiKeyValidatorsMiddleware>());
app.UseMvc(routes =>
{
routes.MapRoute(
name: "Servicedefault",
template: "{controller=Login}/{action=Login}/{ServiceID}/{ServiceName}");
routes.MapRoute(
name: "default",
template: "{controller=Login}/{action=Login}/{id?}");
});
}
Controller Codesnippet
这是一个简单的API,它没有任何身份验证属性。
[Route("api/[controller]")]
public class MoviesAPIController : Controller
{
IMovies _IMovies;
public MoviesAPIController(IMovies imovies)
{
_IMovies = imovies;
}
// GET: api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
// POST api/values
[HttpPost]
public List<MoviesTB> Post([FromQuery]string key)
{
return _IMovies.GetMusicStore();
}
输出
答案 0 :(得分:0)
Map *扩展用作分支管道的约定。映射基于给定请求路径的匹配来分支请求管道。如果请求路径以给定路径开始,则执行分支。
您需要像这样注册您的中间件
app.UseMiddleware<ApiKeyValidatorsMiddleware>();
然后在中间件中检查它是否是API调用。
httpContext.Request.Path.StartsWithSegments("/api")
MiddleWare代码
// You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
public class ApiKeyValidatorsMiddleware
{
private readonly RequestDelegate _next;
IValidateRequest _IValidateRequest { get; set; }
public ApiKeyValidatorsMiddleware(RequestDelegate next, IValidateRequest ivalidaterequest)
{
_next = next;
_IValidateRequest = ivalidaterequest;
}
public async Task Invoke(HttpContext httpContext)
{
try
{
if (httpContext.Request.Path.StartsWithSegments("/api"))
{
var queryString = httpContext.Request.Query;
StringValues keyvalue;
queryString.TryGetValue("key", out keyvalue);
if (httpContext.Request.Method != "POST")
{
httpContext.Response.StatusCode = 405; //Method Not Allowed
await httpContext.Response.WriteAsync("Method Not Allowed");
return;
}
if (keyvalue.Count == 0)
{
httpContext.Response.StatusCode = 400; //Bad Request
await httpContext.Response.WriteAsync("API Key is missing");
return;
}
else
{
var serviceName = httpContext.Request.Path.Value.Replace(@"/", string.Empty);
if (!_IValidateRequest.ValidateKeys(keyvalue) && !_IValidateRequest.IsValidServiceRequest(keyvalue, serviceName))
{
httpContext.Response.StatusCode = 401; //UnAuthorized
await httpContext.Response.WriteAsync("Invalid User Key");
return;
}
}
}
await _next.Invoke(httpContext);
}
catch (Exception)
{
throw;
}
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class MiddlewareExtensions
{
public static IApplicationBuilder UseMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<ApiKeyValidatorsMiddleware>();
}
}