2017-05-01 13 views
1

all要求の応答ヘッダーにコンテンツセキュリティポリシーヘッダーを追加しようとしています。だから私は、その後、各要求のための私のミドルウェアを呼び出すために、私は私のミドルウェア、しかし提案hereすべての要求に対してOWINミドルウェアが呼び出されない

public partial class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     app.Use((context, next) => 
     { 
      var nonceService = ServiceLocator.Current.GetInstance<INonceService>(); 
      var middleware = new SecurityHeaderMiddleware(next, nonceService); 
      return middleware.Invoke(context); 
     }); 

     app.UseStageMarker(PipelineStage.PostResolveCache); 

     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = "Cookies" 
     }); 

     app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions 
     { 
     // set options here 
     }); 



     MvcHandler.DisableMvcResponseHeader = true; 
    } 
} 

あたりとしてstartup.cs before PostResolveCacheステージマーカーで私のミドルウェアを登録OWINミドルウェアに

public class SecurityHeaderMiddleware 
{ 
    private readonly INonceService _nonceService = null; 
    private readonly Func<Task> _next; 
    public SecurityHeaderMiddleware(Func<Task> next, INonceService nonceService) 
    { 
     _nonceService = nonceService; 
     _next = next; 
    } 

    public async Task Invoke(IOwinContext context) 
    {    
     // do something here to add CSP header in context.Response.Headers 

     await _next.Invoke(); 
    } 

を作成しました実際のページまたは任意のAjaxリクエストに対して呼び出されるブラウザがjavascript、CSSまたは画像へのリクエストを作成するときに呼び出されます

カスタムミドルすべてのリクエストのためのウェア? OWINのミドルウェアではない場合は、asp.netのすべてのリクエストにヘッダを追加するオプションがあります

答えて

0

PostAuthorizestage markerは静的コンテンツリクエストを処理するように設定する必要があります。リンクされた問題のコメントでも言及されています。

app.Use(...); 

app.UseStageMarker(PipelineStage.PostAuthorize); 
関連する問題