2015-12-04 36 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() 
      }); 

    }