2015-12-04 80 views
1

我有一個自我託管的OWIN應用程序配置爲授權服務器和信號源資源服務器。解耦自我主機OWIN授權服務器

我的客戶正在成功獲取不記名令牌,並在隨後調用signalR集線器時將其呈現給授權用戶。

我的下一步是分離授權服務,以便它可以在自己的主機上運行。要開始,我創建了一個單獨的自託管應用程序,其中只包含授權服務代碼。它仍然是我開發機器上的一個解決方案,但授權服務和signalR資源託管在單獨的進程中。

驗證流程仍然正常工作。令牌正在進入我的資源服務器,但現在從信號集線器獲得401未授權。

在ASP.Net Web API中有很多支持來解決這個問題,在這個API中你可以在你的web.config文件中同步一個machine.config值。但那不是我的架構。作爲HttpListener下的自託管應用運行,默認情況下使用不同的加密,DPAPI。

在自我託管的體系結構中解決這個問題似乎沒有多少討論。我的理論是,即使在同一臺機器上不同的進程下,DPAPI解密失敗,所以我得到401.

我想弄清楚是否有一些最小的方法來解決這個問題,或者如果我必須完全重構可能會使用JWT。

編輯:添加一些代碼來幫助顯示我的設置

public void ConfigureOAuth(IAppBuilder app) 
{ 
    OAuthAuthorizationServerOptions OAuthServerOptions = new  OAuthAuthorizationServerOptions() 
    { 
     AllowInsecureHttp = false, 
     TokenEndpointPath = new PathString("/account/login"), 
     AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), 
     Provider = new SimpleAuthorizationServerProvider() 
    }; 
    app.UseOAuthAuthorizationServer(OAuthServerOptions); 
} 

public void ConfigureOAuth(IAppBuilder app) 
{ 
    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions 
     { 
      Provider = new ApplicationOAuthBearerAuthenticationProvider(), 
     }); 
} 

回答

3

發佈自己的解決方案希望能夠幫助別人的道路。

我確實決定實施JWT解決方案,而不是使用默認設置。無論如何,我認爲這是更好的體系結構,將令牌加密與操作系統分離。我用這個教程http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/

關鍵的是創建您自定義的OAuthAuthorizationServerProvider和ISecureDataFormat用於加密令牌,如教程中所示。這只是顯示OWIN配置。

public void ConfigureOAuth(IAppBuilder app) 
    { 
     OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() 
     { 
      AllowInsecureHttp = false, 
      TokenEndpointPath = new PathString("/account/login"), 
      AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30), 
      Provider = new JwtAuthorizationServerProvider(), 
      AccessTokenFormat = new CustomJwtFormat("https://foo.test.com") 
     }; 
     app.UseOAuthAuthorizationServer(OAuthServerOptions); 
    } 

你可能面臨的另一個問題是如何將令牌SignalR,其中設置授權頭並不像你想象的那麼簡單。碰巧,本教程中基於cookie的實現也與JWT一起使用得非常好! http://blog.marcinbudny.com/2014/05/authentication-with-signalr-and-oauth.html#.VmWgMXarSCd

再次,這裏是OWIN配置示例。

public void ConfigureOAuth(IAppBuilder app) 
    { 
     //app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions 
     //{ 
     // Provider = new ApplicationOAuthBearerAuthenticationProvider() 
     //}); 

     var issuer = "https://foo.test.com"; 
     var audience = "client_id"; 
     var secret = TextEncodings.Base64Url.Decode("ABCDEF"); 

     // Api controllers with an [Authorize] attribute will be validated with JWT 
     app.UseJwtBearerAuthentication(
      new JwtBearerAuthenticationOptions 
      { 
       AuthenticationMode = AuthenticationMode.Active, 
       AllowedAudiences = new[] { audience }, 
       IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[] 
       { 
        new SymmetricKeyIssuerSecurityTokenProvider(issuer, secret) 
       }, 
       Provider = new ApplicationOAuthBearerAuthenticationProvider() 
      }); 

    }