具有 AllowInsecureHttp=false 的令牌终结点仍接受 http 请求



希望有人可以帮助我解决以下非常令人沮丧的问题。

我有 .NET WebApi,我用 jwt 身份验证保护了它。在设置OAuthAuthorizationServerOptions-object 时,我将令牌端点的属性AllowInsecureHttp设置为false。在IISExpress上本地运行我的API,并使用Postman对其进行测试就像一个魅力。如果我将属性设置为true并在端点上请求令牌,它可以工作,如果我将其设置为false,我会按预期获得404。但是,当我将 API 发布到我的生产环境 (Windows 2012/IIS8) 并将属性设置为false时,我可以通过 httpshttp 获取令牌。它似乎只是不听物业。

我将 API 作为具有别名 API 的应用程序运行,在只有一个 https 绑定的域下。我为我的 API 使用以下帮助程序基类:

public class BaseWebApi<TDerivedOAuthProvider> where TDerivedOAuthProvider : BaseOAuthProvider, new()
{
public BaseWebApi(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(DefaultAuthenticationTypes.ExternalBearer));
ConfigureOAuth(app);
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
private static void ConfigureOAuth(IAppBuilder app)
{
var allowInsecureHttp = Convert.ToBoolean(ConfigurationManager.AppSettings["jwt::allowInsecureHttp"].ToString());
var issuer = ConfigurationManager.AppSettings["jwt::issuer"].ToString();
var secret = TextEncodings.Base64Url.Decode(ConfigurationManager.AppSettings["jwt::secret"].ToString());
var clients = ConfigurationManager.AppSettings["jwt::clients"].ToString().Split(new char[] { ',' });
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = allowInsecureHttp,
AuthenticationType = DefaultAuthenticationTypes.ExternalBearer,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(10),
Provider = new TDerivedOAuthProvider(),
RefreshTokenProvider = new BaseRefreshTokenProvider(),
AccessTokenFormat = new BaseJwtFormat(issuer),
};
// OAuth 2.0 Bearer Access Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
// Api controllers with an [Authorize] attribute will be validated with JWT
app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ExternalBearer,
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = clients,
IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
{
new SymmetricKeyIssuerSecurityTokenProvider(issuer, secret)
}
});
}
}

变量allowInsecureHttpissuersecretclients都是从配置文件填充的。将硬编码allowInsecureHttp的值设置为falsetrue不会更改任何内容。然后,这个基类由我的实际 API 实例化,该 API (在实际 API 函数旁边)还提供了一个处理此特定 API 凭据检查的实际实现的CustomOAuthProvider类:

[assembly: OwinStartup(typeof(MyCustomAPI.Startup))]
namespace MyCustomAPI.API
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
BaseWebApi<CustomOAuthProvider> webApi = new BaseWebApi<CustomOAuthProvider>(app);
}
}
}

希望任何人都可以为我指出正确的方向。撇开这个问题不谈,API 本身运行良好,但我真的很想在我的生产令牌上强制使用 SSL。

干杯 奈奎斯特

对于遇到相同问题的任何人。最终想通了。像魅力一样工作,但是我配置虚拟应用程序的域上的HSTS策略将所有http Postman流量自动重定向到https。在没有SSL的简单域上安装了应用程序,并且在该域上完美地执行了SSL。叹息:)

最新更新