在ASP.NET和ASP.NET Core中禁用访问控制

发布于 2025-01-31 18:30:24 字数 398 浏览 3 评论 0原文

我们刚刚进行了外部笔测试,我们所有的网站都回来了,警告说我们允许跨站点脚本。 我认为实际上并非如此,因为我们必须在一个特定站点的一个页面上专门允许它才能工作。

该报告表明,当调用我们的URL是访问控制的标题时,设置为 *。 使用邮递员,我可以得到相同的结果。

这两个ASP.NET Web表单应用程序以及新的ASP.NET 6 Razor Page Apps都返回了相同的结果。 有什么办法可以删除此标头? 也许是IIS中的东西?

We just had an external pen test and all of our sites are coming back with a low warning stating that we allow cross site scripting.
I don't think this is actually the case since we had to specifically allow it on one page on one specific site for that one to work.

The report shows that when calling our URL's a header for Access-Control-Allow-Origin is set to *.
Using Postman I can get that same result.

PostMan Screenshot

This is returning the same result from both ASP.Net web forms applications as well as new ASP.Net 6 Razor page apps.
Is there any way to have this header removed?
Maybe something in IIS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流殇 2025-02-07 18:30:25

要摆脱它,您必须列出所有允许将请求发送到您的端点的起源。如果您正在运行ASP.NET Core应用程序,则必须以这样的方式配置CORS中间件:

// Startup.ConfigureServices() method

// For example only, put these values in the appsettings.json so they could be overridden if you need it
var corsAllowAnyOrigin = false;
var corsAllowOrigins = new string[]{ "https://*.contoso.com", "https://api.contoso.com" };

// Configuring CORS module
services.AddCors(options =>
{
    options.AddDefaultPolicy(
        builder =>
        {
            if (apiConfiguration.CorsAllowAnyOrigin)
            {
                builder.AllowAnyOrigin();
            }
            else
            {
                builder.WithOrigins(apiConfiguration.CorsAllowOrigins);
            }

            builder.AllowAnyHeader();
            builder.AllowAnyMethod();
        });
});

对于您的Web表单应用程序,您可以安装IIS CORS模块并在web.config文件中进行配置。

<?xml version="1.0"?>
<configuration>
  <system.webServer>
    <cors enabled="true">
      <add origin="*" allowed="false"/>
      <add origin="https://*.contoso.com" allowCredentials="false" />
      <add origin="https://api.contoso.com" allowCredentials="true" />
    </cors>
  </system.webServer>
</configuration>

To get rid of it you have to list all the origins that are allowed to send the requests to your endpoint. If you are running ASP.NET Core application then you have to configure the CORS middleware like this:

// Startup.ConfigureServices() method

// For example only, put these values in the appsettings.json so they could be overridden if you need it
var corsAllowAnyOrigin = false;
var corsAllowOrigins = new string[]{ "https://*.contoso.com", "https://api.contoso.com" };

// Configuring CORS module
services.AddCors(options =>
{
    options.AddDefaultPolicy(
        builder =>
        {
            if (apiConfiguration.CorsAllowAnyOrigin)
            {
                builder.AllowAnyOrigin();
            }
            else
            {
                builder.WithOrigins(apiConfiguration.CorsAllowOrigins);
            }

            builder.AllowAnyHeader();
            builder.AllowAnyMethod();
        });
});

For your Web Forms application you can install IIS CORS module and configure it in the web.config file like this:

<?xml version="1.0"?>
<configuration>
  <system.webServer>
    <cors enabled="true">
      <add origin="*" allowed="false"/>
      <add origin="https://*.contoso.com" allowCredentials="false" />
      <add origin="https://api.contoso.com" allowCredentials="true" />
    </cors>
  </system.webServer>
</configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文