2016-04-08 15 views
0

私は数日間、OWIN cookieミドルウェア(3.0.1)の奇妙な問題で苦労しています。OWIN web.configの認証要素を持つCookieミドルウェア

最小限のコード例で問題を再現しました。最初に、空のASP.NET Webアプリケーションプロジェクト(VS.​​NET 2015)を作成します。そしてOWINのnugetパッケージ等...そして、ここで基本的なスタートアップクラス:予想通り、私はweb.configファイルに次の要素を追加するまで

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); 

     app.Use(async (context, next) => 
     { 
      // context.Response.StatusCode = 200 here 
      Debug.WriteLine("1. ==> Request - Before Cookie Auth"); 
      await next(); 
      // context.Response.StatusCode = 401 here 
      Debug.WriteLine("4. <== Response - After Cookie Auth"); 
     }); 

     app.UseCookieAuthentication(new CookieAuthenticationOptions()); 

     // by adding this element to web.config, the next middle is no longer used, the request ends with 401.2 
     // <authorization> 
     // <deny users="?" /> 
     // </authorization> 

     app.Use(async (context, next) => 
     { 
      // I thought this middleware would be hit so it could deal with 401 - issue challenge 
      // e.g. app.UseOpenIdConnectAuthentication etc... 
      Debug.WriteLine("2. ==> After Cookie Auth, Never Hit!! if web.config has <authorization>< deny users = \"?\" /> "); 
      await next(); 
      Debug.WriteLine("3. <== Never hit"); 
     }); 
    } 
} 

コードが動作する

<authorization> 
    <deny users="?" /> 
</authorization> 

私の第二OWINミドルウェア今すぐ呼び出されることはありません。この例では、単純なロギングステートメントですが、より大きなプロジェクトではそのapp.UseOpenIdConnectAuthenticationが使用されます。

私の理解では、401のステータスは他の認証ミドルウェアによって処理され、チャレンジレスポンスが作成されることを理解しました。

しかし、クッキーミドルウェアが呼び出された後は何もありません。応答は401で返されます。

これは、ローカルIISとIIS Expressの両方で発生します。これは、統合されたパイプラインで実行するようにOwinを指示します

app.UseStageMarker(PipelineStage.Authenticate); 

答えて

1

はapp.UseCookieAuthentication後に次のコードを追加します。

関連する問題