【问题标题】:Token endpoint with AllowInsecureHttp=false still accepting http requestsAllowInsecureHttp=false 的令牌端点仍然接受 http 请求
【发布时间】:2017-11-20 03:11:25
【问题描述】:

希望任何人都可以帮助我解决以下令人沮丧的问题。

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

我在一个只有一个 https 绑定的域下将 API 作为具有别名 Api 的应用程序运行。我为我的 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 函数旁边)还提供了一个 CustomOAuthProvider 类来处理这个特定 API 的凭据检查的实际实现:

[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。

干杯, 奈奎斯特

【问题讨论】:

    标签: c# asp.net asp.net-web-api jwt access-token


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-12-10
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      相关资源
      最近更新 更多