2017-12-28 72 views
0

에 두 번 호출 : 내 Startup.Configuration에 쿠키 인증 미들웨어를 추가OWIN AuthenticationMiddleware는 owin 쿠키 인증 미들웨어를 사용하는 동안 나는 문제가 요청

, 내 미들웨어의 모든 요청에 ​​두 번 호출됩니다, 이것은 내입니다 코드 : 다음

public void Configuration(IAppBuilder app) 
    { 
     app.Use((context, next) => 
     { 
      // this code will be executed twice per http request 
      return next(); 
     }); 

     // my cookie middleware 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = CookieAuthenticationDefaults.AuthenticationType, 
      AuthenticationMode = AuthenticationMode.Active 
     }); 
    } 

와 나는 구현 빈과 AuthenticationMiddleware에서 상속 자신 미들웨어를 writed, 그냥 같은 일이야 - 내 미들웨어의 모든 요청에 ​​두 번 호출합니다.

public override async Task Invoke(IOwinContext context) 
    { 
     AuthenticationHandler<TOptions> handler = CreateHandler(); 
     await handler.Initialize(Options, context); 
     if (!await handler.InvokeAsync()) 
     { 
      await Next.Invoke(context); 
     } 
     await handler.TeardownAsync(); 
    } 

내가 await Next.Invoke(context); 다시 호출 내 미들웨어의 모든 원인이됩니다 발견 :

은 내가 owin AuthenticationMiddleware 소스 코드를 디버깅 할 때.

하지만 다른 미들웨어 (StaticFiles 미들웨어, Webapi 미들웨어)를 사용하면 모든 것이 잘됩니다.

OWIN 또는 AuthenticationMiddleware에 대해 뭔가를 놓쳤습니까?

요청 당 내 미들웨어가 두 번 호출되는 이유는 무엇입니까? 그것은 설계에 의한 것인가?

답변

0

모든 응용 프로그램이 Owin에 있으면이 미들웨어를 파이프 라인의 끝에 추가해야하며 문제는 없어야합니다!

app.Run(async context => 
{ 
    context.Response.StatusCode = 404; 
    await context.Response.WriteAsync("404"); 
}); 

이렇게하면 요청이 owin 컨텍스트에서 유지되고 기본 IIS 모듈이 생성되지 않습니다. 웬일인지 (나는 모른다!) 요청이 Owin에서 나오고 어떤 모듈도 처리하지 않으면, owin 요청은 두 번 실행된다 !!

+0

예, 마지막 오우 인 미들웨어에서 요청을 처리해야합니다. 미들웨어가 요청을 처리하지 않으면 (그리고 마지막 미들웨어가 next();를 반환합니다), 모든 미들웨어가 다시 호출됩니다. – acmajia