2017-07-03 4 views
0

OWINでHTTPListenerを使用してasp.net webapiをWindowsサービスとしてホストしています.1つの部分を条件付きで認証したいと思います。 OAuth2でアプリケーションのWindows認証と残りの部分でセキュリティ保護されています。これはIISでは簡単ですが、OWIN + HttpListenrではそれほど簡単ではありません。Windows認証でアプリケーションの/ secureを条件付きで認証するOAuth2でアプリケーションの残りの部分を認証する

私はセットアップにWindows認証をこのarticleに続くが、それはあなたには、いくつかの条件付きロジックを追加するHttpListener.AuthenticationSchemeSelectorDelegateを使用することができ、アプリケーション全体

namespace KatanaSelfHost 
    { 
     class Startup 
     { 
      public void Configuration(IAppBuilder app) 
      { 
       HttpListener listener = 
        (HttpListener)app.Properties["System.Net.HttpListener"]; 
       listener.AuthenticationSchemes = 
        AuthenticationSchemes.IntegratedWindowsAuthentication; 

       app.Run(context => 
       { 
        context.Response.ContentType = "text/plain"; 
        return context.Response.WriteAsync("Hello World!"); 
       }); 
      } 
     } 
    } 

答えて

0

ためです。

namespace KatanaSelfHost 
    { 
     class Startup 
     { 
      public void Configuration(IAppBuilder app) 
      { 
       HttpListener listener = 
        (HttpListener)app.Properties["System.Net.HttpListener"];      
listener.AuthenticationSchemeSelectorDelegate = 
    new AuthenticationSchemeSelector (AuthenticationSchemeForClient); 


       app.Run(context => 
       { 
        context.Response.ContentType = "text/plain"; 
        return context.Response.WriteAsync("Hello World!"); 
       }); 
      } 
     } 
    } 
    static AuthenticationSchemes AuthenticationSchemeForClient(HttpListenerRequest request) 
    { 
     Console.WriteLine("Client authentication protocol selection in progress..."); 
     // check the url here 
     if (request.RawUrl.Contains("/secure")) 
     { 
      return AuthenticationSchemes.IntegratedWindowsAuthentication; 
     } 
     else 
     { 
      return AuthenticationSchemes.None; 
     } 
    } 
関連する問題