我们的安全团队要求所有cookies都设置为Secure=true。
为了设置MVC AntiForgery的安全属性,我们使用以下代码:
protected void Application_BeginRequest(object sender, EventArgs e)
{
AntiForgeryConfig.RequireSsl = HttpContext.Current.Request.IsSecureConnection;
} 但是现在我们在测试服务器上遇到了一个问题,它没有使用SSL。有时我们会有一些自发的错误
The anti-forgery system has the configuration value AntiForgeryConfig.RequireSsl = true, but the current request is not an SSL request.在查看ASP.NET MVC代码以确定异常的位置时,我们发现了以下内容
private void CheckSSLConfig(HttpContextBase httpContext)
{
if (_config.RequireSSL && !httpContext.Request.IsSecureConnection)
{
throw new InvalidOperationException(WebPageResources.AntiForgeryWorker_RequireSSL);
}
}它看起来是正确的,应该可以工作,因为执行顺序是
AntiForgeryConfig.RequireSsl = HttpContext.Current.Request.IsSecureConnection;
// ... something happens in between
if (_config.RequireSSL && !httpContext.Request.IsSecureConnection)
{
throw new InvalidOperationException(WebPageResources.AntiForgeryWorker_RequireSSL);
}但是对于某些请求,HttpContext.Current.Request.IsSecureConnection似乎返回了true,尽管我们在测试服务器上没有使用SSL。
那是怎么回事?为什么我们会得到这个异常?
发布于 2016-07-12 18:08:52
我正在搜索有关AntiForgeryConfig.RequireSsl的信息,找到了您的问题。在您的以下代码中:
protected void Application_BeginRequest(object sender, EventArgs e)
{
AntiForgeryConfig.RequireSsl = HttpContext.Current.Request.IsSecureConnection;
} 您可以使用本地值(Request.IsSecureConnection)修改应用程序级值(AntiForgeryConfig.RequireSsl)。
如果你有两个不同Request.IsSecureConnection值的请求,你认为会发生什么?-第一个请求将AntiForgeryConfig.RequireSsl设置为false -第二个请求将AntiForgeryConfig.RequireSsl设置为true -第一个请求由CheckSSLConfig (true)评估-第二个请求由CheckSSLConfig (true)评估
您必须避免以这种方式修改全局应用程序设置,并编写自己的筛选器来处理此类行为。
https://stackoverflow.com/questions/17970174
复制相似问题