首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当不使用SSL时,AntiForgeryConfig.RequireSsl会导致随机错误。

当不使用SSL时,AntiForgeryConfig.RequireSsl会导致随机错误。
EN

Stack Overflow用户
提问于 2013-07-31 20:17:23
回答 1查看 3.6K关注 0票数 2

我们的安全团队要求所有cookies都设置为Secure=true。

为了设置MVC AntiForgery的安全属性,我们使用以下代码:

代码语言:javascript
复制
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        AntiForgeryConfig.RequireSsl = HttpContext.Current.Request.IsSecureConnection;
    } 

但是现在我们在测试服务器上遇到了一个问题,它没有使用SSL。有时我们会有一些自发的错误

代码语言:javascript
复制
The anti-forgery system has the configuration value AntiForgeryConfig.RequireSsl = true, but the current request is not an SSL request.

在查看ASP.NET MVC代码以确定异常的位置时,我们发现了以下内容

代码语言:javascript
复制
private void CheckSSLConfig(HttpContextBase httpContext)
{
    if (_config.RequireSSL && !httpContext.Request.IsSecureConnection)
    {
        throw new InvalidOperationException(WebPageResources.AntiForgeryWorker_RequireSSL);
    }
}

它看起来是正确的,应该可以工作,因为执行顺序是

代码语言:javascript
复制
    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。

那是怎么回事?为什么我们会得到这个异常?

EN

回答 1

Stack Overflow用户

发布于 2016-07-12 18:08:52

我正在搜索有关AntiForgeryConfig.RequireSsl的信息,找到了您的问题。在您的以下代码中:

代码语言:javascript
复制
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)评估

您必须避免以这种方式修改全局应用程序设置,并编写自己的筛选器来处理此类行为。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17970174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档