我已经创建了一个带有 OAuth token 身份验证的 Web API 应用程序。当 token 服务器与服务在同一应用程序上运行时,这没有问题。但是,我想将授权服务移动到它自己的应用程序(VS 项目)中,并在我正在处理的几个 Web API 项目中使用它。但是,当我将授权逻辑隔离到它自己的项目中时,原始服务不再将生成的 token 视为有效。我的问题是,一个 Web API 项目是否可以为另一个项目生成 token 以供验证?这是我的身份验证服务和原始服务的 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 的答案是 token 是使用 machine.config 文件中的 machineKey 属性生成的:如果你想在多台服务器上托管,你需要覆盖它。
可以在 web.config 中覆盖 MachineKey :
<system.web>
<machineKey validationKey="VALUE GOES HERE"
decryptionKey="VALUE GOES HERE"
validation="SHA1"
decryption="AES"/>
</system.web>
机器 key 应在本地生成 - 使用在线服务不安全。 KB Article for generating keys
此处所有内容的原始引用 http://bitoftech.net/2014/09/24/decouple-owin-authorization-server-resource-server-oauth-2-0-web-api
关于c# - 跨两个 Web API 项目共享 OAuth token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25749818/