【发布时间】: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)
}
});
}
}
变量allowInsecureHttp、issuer、secret 和clients 是从配置文件中填充的。将allowInsecureHttp 硬编码的值设置为false 或true 不会改变任何内容。然后这个基类由我的实际 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