1 回答
TA贡献1811条经验 获得超6个赞
首先让我简单介绍一下OWIN的工作原理。值得您阅读更多有关它的信息,但这是一个很好的起点。
将OWIN视为请求和应用程序之间的管道,本例中为MVC Web Api。流水线的每个部分都称为“中间件”,请求流经此流水线,直到到达您的应用程序为止。当请求到达管道时,流程反向,数据流回通过管道。因此,当数据进入应用程序以及何时离开应用程序时,该管道的每个部分(“中间件”)都会看到两次数据。中间件可以在请求进入时查看或修改请求,或者在离开应用程序时查看或修改响应,或两者。
每个中间件组件都接收一个字典,其中包含有关请求和响应的各种数据,以及一个调用下一个管道的委托。
现在为您的代码:
您的应用程序是WebApi,由以下几行定义:
app.UseWebApi(AppConfiguration);
您可以使用以下几行来初始化两个中间件:
var issuer = ConfigurationManager.AppSettings["as:IssuerServer"];
var tokenEndpoint = ConfigurationManager.AppSettings["as:OwinTokenEndpoint"]; // "/oauth/token"
OAuthAuthorizationServerOptions oAuthServerOptions = new OAuthAuthorizationServerOptions()
{
////For Dev enviroment only (on production should be AllowInsecureHttp = false)
AllowInsecureHttp = true,
TokenEndpointPath = new Microsoft.Owin.PathString(tokenEndpoint),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new CustomOAuthProvider(),
AccessTokenFormat = new CustomJwtFormat(issuer)
};
// OAuth 2.0 Bearer Access Token Generation
this.app.UseOAuthAuthorizationServer(oAuthServerOptions);
然后这些行:
string audienceId = ConfigurationManager.AppSettings["as:AudienceId"];
var issuer = ConfigurationManager.AppSettings["as:IssuerServer"]; // Should have the Url of the auth server http://localhost:53025/";
byte[] audienceSecret = TextEncodings.Base64Url.Decode(ConfigurationManager.AppSettings["as:AudienceSecret"]);
this.app.UseJwtBearerAuthentication(
new JwtBearerAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = new[] { audienceId },
IssuerSecurityKeyProviders = new IIssuerSecurityKeyProvider[]
{
new SymmetricKeyIssuerSecurityKeyProvider(issuer, audienceSecret)
}
});
该中间件将始终运行。不管您是否在任何地方都有任何属性。
您说该错误在本地开发计算机上不会发生,这意味着您很可能遇到配置问题。您正在以上引用的行中将设置传递到中间件中。
基于此,我怀疑您对issuer或tokenEndpoint变量或(更可能是)audienceId或audienceSecret变量有问题。
希望能有所帮助。
- 1 回答
- 0 关注
- 239 浏览
添加回答
举报