如何允许CORS在ASP.NET Core Web API中的生产

发布于 2025-02-09 02:09:25 字数 1830 浏览 1 评论 0原文

我有两个ASP.NET站点,一个是REST Web API。我正在从另一个网站向API提出请求,以获取PDF并使用Adobe Acrobat显示。以下是我如何配置Web API在生产中使用启动文件中使用CORS,因为这些站点将托管在一起,因此现在我想允许CORS;

app.UseCors(x => x
        .AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader());

我有一个错误

'https:// localhost:44389/documents/test.pdf'froment'https:// localhost:44382'访问XMLHTTPREQUEST:44389/documents/test.pdf'标题存在于请求的资源

您可以看到,我已经配置了CORS以从任何原点接受,除了所有API调用以获取数据的呼叫都可以正常工作。以下是使用JavaScript获取文档的代码,以与Acrobat Reader一起使用。

<script>
const viewerConfig = {
    /* Allowed possible values are "FIT_PAGE", "FIT_WIDTH" or "" */
    defaultViewMode: "",
};

/* Wait for Adobe Document Services PDF Embed API to be ready */
document.addEventListener("adobe_dc_view_sdk.ready", function () {
    /* Initialize the AdobeDC View object */
    var adobeDCView = new AdobeDC.View({
        /* Pass your registered client id */
        clientId: "dc02e57f9741b5a2ccd748bfc012ae3",
        /* Pass the div id in which PDF should be rendered */
        divId: "adobe-dc-view",
    });

    /* Invoke the file preview API on Adobe DC View object */
    adobeDCView.previewFile({
        /* Pass information on how to access the file */
        content: {
            /* Location of file where it is hosted */
            location: {
                url: "https://localhost:44389/documents/test.pdf",


            },
        },
        /* Pass meta data of file */
        metaData: {
            /* file name */
            fileName: "@viewDocument.DocumentTitle"
        }
    }, {
        defaultViewMode: "FIT_WIDTH", showAnnotationTools: true, showLeftHandPanel: false,
        showDownloadPDF: false, showPrintPDF: false
    });
});
</script>

我的CORS设置错误还是我错过了什么?

I have two ASP.NET sites and one is a REST Web API. I am making a request to the API from the other website to get a PDF and display using Adobe Acrobat. Below is how I have configured the web API to use CORS in my startup file in production because these sites will be hosted together so for now I want to allow CORS;

app.UseCors(x => x
        .AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader());

I get an error

Access to XMLHttpRequest at 'https://localhost:44389/documents/test.pdf' from origin 'https://localhost:44382' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

As you can see I have configured CORS to accept from any origin and apart from that all API calls to fetch data into datatables are working. Below is the code to fetch the document using Javascript to use with Acrobat reader.

<script>
const viewerConfig = {
    /* Allowed possible values are "FIT_PAGE", "FIT_WIDTH" or "" */
    defaultViewMode: "",
};

/* Wait for Adobe Document Services PDF Embed API to be ready */
document.addEventListener("adobe_dc_view_sdk.ready", function () {
    /* Initialize the AdobeDC View object */
    var adobeDCView = new AdobeDC.View({
        /* Pass your registered client id */
        clientId: "dc02e57f9741b5a2ccd748bfc012ae3",
        /* Pass the div id in which PDF should be rendered */
        divId: "adobe-dc-view",
    });

    /* Invoke the file preview API on Adobe DC View object */
    adobeDCView.previewFile({
        /* Pass information on how to access the file */
        content: {
            /* Location of file where it is hosted */
            location: {
                url: "https://localhost:44389/documents/test.pdf",


            },
        },
        /* Pass meta data of file */
        metaData: {
            /* file name */
            fileName: "@viewDocument.DocumentTitle"
        }
    }, {
        defaultViewMode: "FIT_WIDTH", showAnnotationTools: true, showLeftHandPanel: false,
        showDownloadPDF: false, showPrintPDF: false
    });
});
</script>

Are my CORS settings wrong or am I missing something?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文