asp에서 사용자 정의 인증 미들웨어를 만들었습니다.조건부로 사용자 정의 미들웨어 사용
public class MyAuthenticationMidleware
{
private readonly RequestDelegate _next;
public ConnectAuthenticationMidleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
if (!UserIsAuthenticated())
{
context.Response.StatusCode = 401;
return;
}
...
await _next.Invoke(context);
}
}
public static class MyAuthenticationMidlewareExtensions
{
public static IApplicationBuilder UseMyAuthentication(this IApplicationBuilder builder)
{
return builder.UseMiddleware<MyAuthenticationMidleware>();
}
}
시작으로 : - 인증 미들웨어는 각 요청에 대해 실행이 제대로 작동
public void Configure(...)
{
app.UseStaticFiles();
app.UseMyAuthentication();
app.UseMvc();
}
아래 그림과 같이 순 핵심 프로젝트, 그리고 등록. 사용자가 인증되지 않으면 401이 리턴됩니다. 그렇지 않으면 특정 mvc 조치가 호출됩니다.
내가 시도한 것은 인증 미들웨어가 특정 작업을 수행하는 것을 막는 것이 었습니다. 나는 또 다른 확장 메서드를 만들 MapWhen
방법을 사용하고 다음과 같이 사용 : 예상대로
public static class MyAuthenticationMidlewareExtensions
{
public static IApplicationBuilder UseMyAuthentication(this IApplicationBuilder builder)
{
return builder.UseMiddleware<MyAuthenticationMidleware>();
}
public static IApplicationBuilder UseMyAuthenticationWhen(this IApplicationBuilder builder, Func<HttpContext, bool> predicate)
{
return builder.MapWhen(predicate, applicationBuilder => applicationBuilder.UseMyAuthentication());
}
}
public void Configure(...)
{
app.UseStaticFiles();
app.UseMyAuthenticationWhen(context => context.Request.Path != "xyz");
app.UseMvc();
}
불행하게도,이 작동하지 않습니다. 미들웨어는 경로가 "xyz"와 다른 경우에만 호출되지만 전체 체인을 짧게 순환시키는 것으로 보이며 특정 작업이나 필터는 호출되지 않습니다.
아마 MapWhen
에 대한 나의 이해가 잘못되었습니다. 원하는 결과를 얻을 수있는 방법이 있습니까?