二回呼び出さコード: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 - 私のミドルウェアのすべてのリクエストごとに二回起動されます。私はowin AuthenticationMiddlewareをソースコードデバッグするとき
は:
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);
が私のミドルウェアのすべてが再び呼び出さ原因となりますことを見出しました。
しかし、他のミドルウェア(StaticFilesミドルウェア、Webapiミドルウェア)を使用すると、すべて正常に動作します。
私はOWINまたはAuthenticationMiddlewareについて何か忘れましたか?
ミドルウェアがリクエストごとに2回呼び出されるのはなぜですか?それは設計によるのですか?
はい、私は最後のミドルウェアでリクエストを処理する必要があることがわかりました。ミドルウェアが要求を処理していない場合(そして最後のミドルウェアがnext();を返した場合)、すべてのミドルウェアが再度呼び出されます。 – acmajia