C#学习教程:在两个WebAPI项目之间共享一个OAuth令牌当令牌服务器与服务在同一应用程序上运行时,这没有问题。但是,我想将授权服务移动到我自己的应用程序(VS项目)中,并将其用于我正在处理的几个WebAPI项目。但是,当我将授权逻辑隔离到它自己的项目中时,原始服务不再认为生成的令牌有效。我的问题是,一个WebAPI项目是否可以为另一个WebAPI项目生成令牌以进行身份??验证?这是我的授权服务和原始身份验证服务的OWIN启动代码:publicvoidConfiguration(IAppBuilderapp){//有关如何配置应用程序的更多信息,请访问http://go.microsoft.com/fwlink/?LinkID=316888HttpConfigurationconfig=newHttpConfiguration();配置OAuth(app);WebApiConfig.Register(配置);app.UseWebApi(配置);app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);}privatevoidConfigureOAuth(IAppBuilderapp){OAuthAuthorizationServerOptionsOAuthServerOptions=newOAuthAuthorizationServerOptions(){AllowInsecureHttp=true,TokenEndpointPath=newPathString("/token"),AccessTokenExpireTimeSpan=TimeSpan.FromDays(1),Provider=newSimpleAuthorizationServerProvider()};//令牌生成应用程序。UseOAuthAuthorizationServer(OAuthServerOptions);app.UseOAuthBearerAuthentication(新的OAuthBearerAuthenticationOptions());}原始服务:publicvoidConfiguration(IAppBuilderapp){ConfigureOAuth(应用程序);//有关如何配置应用程序的详细信息,请访问http://go.microsoft.com/fwlink/?LinkID=316888HttpConfigurationconfig=newHttpConfiguration();配置.SuppressDefaultHostAuthentication();config.Filters.Add(newHostAuthenticationFilter(OAuthDefaults.AuthenticationType));WebApiConfig.Register(配置);app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);app.UseWebApi(配置);}publicvoidConfigureOAuth(IAppBuilderapp){varoauthBearerOptions=newOAuthBearerAuthenticationOptions();app.UseOAuthBearerAuthentication(oauthBearerOptions);我自己研究这个问题时偶然发现了这个问题TL;DR答案是使用machine.config文件中的machineKey属性生成令牌:托管在服务器上,您需要覆盖它。MachineKey可以在web.config中被覆盖:MachineKey应该在本地生成——使用在线服务是不安全的。密钥生成知识库文章所有这些的原始参考http://bitoftech.net/2014/09/24/decouple-owin-authorization-server-resource-server-oauth-2-0-web-api以上是C#学习教程:分享跨两个WebAPI项目共享OAuthtoken的全部内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
