跨两个Web API项目共享OAuth令牌

我创建了一个带有OAuth令牌认证的Web API应用程序。 当令牌服务器在与服务相同的应用程序上运行时,这没有问题。 但是,我想将授权服务移动到自己的应用程序(VS项目)中,并将其用于我正在处理的几个Web API项目中。 但是,当我将授权逻辑隔离到它自己的项目中时,原始服务不再将生成的令牌视为有效。 我的问题是,一个Web API项目是否有可能为另一个Web API项目生成令牌以进行validation? 这是我的授权服务和原始服务的OWIN启动代码

认证服务:

public void Configuration(IAppBuilder app) { // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888 HttpConfiguration config = new HttpConfiguration(); ConfigureOAuth(app); WebApiConfig.Register(config); app.UseWebApi(config); app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); } private void ConfigureOAuth(IAppBuilder app) { OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() { AllowInsecureHttp = true, TokenEndpointPath = new PathString("/token"), AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), Provider = new SimpleAuthorizationServerProvider() }; // Token Generation app.UseOAuthAuthorizationServer(OAuthServerOptions); app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); } 

原始服务:

 public void Configuration(IAppBuilder app) { ConfigureOAuth(app); // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888 HttpConfiguration config = new HttpConfiguration(); config.SuppressDefaultHostAuthentication(); config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); WebApiConfig.Register(config); app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); app.UseWebApi(config); } public void ConfigureOAuth(IAppBuilder app) { var oauthBearerOptions = new OAuthBearerAuthenticationOptions(); app.UseOAuthBearerAuthentication(oauthBearerOptions); } 

在我自己研究这个问题时偶然发现了这个问题。 TL; DR答案是使用machine.config文件中的machineKey属性生成标记:如果要在多个服务器上托管,则需要覆盖它。

MachineKey可以在web.config中重写:

    

机器密钥应在本地生成 – 使用在线服务并不安全。 KB用于生成密钥的文章

所有这些的原始参考http://bitoftech.net/2014/09/24/decouple-owin-authorization-server-resource-server-oauth-2-0-web-api