You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
23 KiB
JavaScript

"use strict";(self.webpackChunkfurion=self.webpackChunkfurion||[]).push([[8133],{4113:(e,n,s)=>{s.r(n),s.d(n,{assets:()=>t,contentTitle:()=>d,default:()=>a,frontMatter:()=>c,metadata:()=>l,toc:()=>o});var r=s(5893),i=s(1151);const c={id:"cors",title:"16. CORS \u8de8\u57df",sidebar_label:"16. CORS \u8de8\u57df"},d=void 0,l={id:"cors",title:"16. CORS \u8de8\u57df",description:"16.1 \u4ec0\u4e48\u662f\u8de8\u57df",source:"@site/docs/cors.mdx",sourceDirName:".",slug:"/cors",permalink:"/docs/cors",draft:!1,unlisted:!1,editUrl:"https://gitee.com/dotnetchina/Furion/tree/v4/handbook/docs/cors.mdx",tags:[],version:"current",lastUpdatedBy:"\u767e\u5c0f\u50e7",lastUpdatedAt:1699252005,formattedLastUpdatedAt:"Nov 6, 2023",frontMatter:{id:"cors",title:"16. CORS \u8de8\u57df",sidebar_label:"16. CORS \u8de8\u57df"},sidebar:"docs",previous:{title:"15. \u5b89\u5168\u9274\u6743",permalink:"/docs/auth-control"},next:{title:"17. \u89c6\u56fe/\u6a21\u677f\u5f15\u64ce",permalink:"/docs/view-engine"}},t={},o=[{value:"16.1 \u4ec0\u4e48\u662f\u8de8\u57df",id:"161-\u4ec0\u4e48\u662f\u8de8\u57df",level:2},{value:"16.2 \u6709\u8de8\u57df\u884c\u4e3a\u793a\u4f8b",id:"162-\u6709\u8de8\u57df\u884c\u4e3a\u793a\u4f8b",level:2},{value:"16.3 \u4ec0\u4e48\u662f CORS",id:"163-\u4ec0\u4e48\u662f-cors",level:2},{value:"16.4 \u5982\u4f55\u4f7f\u7528",id:"164-\u5982\u4f55\u4f7f\u7528",level:2},{value:"16.4.1 \u6dfb\u52a0 <code>CORS</code> \u670d\u52a1",id:"1641-\u6dfb\u52a0-cors-\u670d\u52a1",level:3},{value:"16.4.2 \u914d\u7f6e\u5141\u8bb8\u8de8\u57df\u57df\u540d",id:"1642-\u914d\u7f6e\u5141\u8bb8\u8de8\u57df\u57df\u540d",level:3},{value:"16.5 <code>CorsAccessorSettings</code> \u914d\u7f6e",id:"165-corsaccessorsettings-\u914d\u7f6e",level:2},{value:"16.6 \u524d\u7aef\u4e0d\u80fd\u8bfb\u53d6\u54cd\u5e94\u5934\u6ce8\u610f\u4e8b\u9879",id:"166-\u524d\u7aef\u4e0d\u80fd\u8bfb\u53d6\u54cd\u5e94\u5934\u6ce8\u610f\u4e8b\u9879",level:2},{value:"16.7 \u4f7f\u7528 <code>$.ajax</code> \u524d\u7aef\u6ce8\u610f\u4e8b\u9879",id:"167-\u4f7f\u7528-ajax-\u524d\u7aef\u6ce8\u610f\u4e8b\u9879",level:2},{value:"16.8 \u7981\u7528\u8de8\u57df",id:"168-\u7981\u7528\u8de8\u57df",level:2},{value:"16.9 <code>SignalR</code> \u8de8\u57df\u95ee\u9898",id:"169-signalr-\u8de8\u57df\u95ee\u9898",level:2},{value:"16.10 \u9759\u6001\u8d44\u6e90\u8de8\u57df\u95ee\u9898",id:"1610-\u9759\u6001\u8d44\u6e90\u8de8\u57df\u95ee\u9898",level:2},{value:"16.11 \u53cd\u9988\u4e0e\u5efa\u8bae",id:"1611-\u53cd\u9988\u4e0e\u5efa\u8bae",level:2}];function h(e){const n={a:"a",admonition:"admonition",code:"code",h2:"h2",h3:"h3",hr:"hr",li:"li",p:"p",pre:"pre",strong:"strong",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",ul:"ul",...(0,i.a)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(n.h2,{id:"161-\u4ec0\u4e48\u662f\u8de8\u57df",children:"16.1 \u4ec0\u4e48\u662f\u8de8\u57df"}),"\n",(0,r.jsxs)(n.p,{children:["\u7b80\u5355\u6765\u8bf4\uff0c\u5f53\u4e00\u4e2a\u8bf7\u6c42 ",(0,r.jsx)(n.code,{children:"url"})," \u7684\u534f\u8bae\u3001\u57df\u540d\u3001\u7aef\u53e3\u4e09\u8005\u4e4b\u95f4\u4efb\u610f\u4e00\u4e2a\u4e0e\u5f53\u524d\u9875\u9762 ",(0,r.jsx)(n.code,{children:"url"})," \u4e0d\u540c\u5373\u4e3a\u8de8\u57df\u3002\u90a3\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u8de8\u57df\u95ee\u9898\u5462\uff1f"]}),"\n",(0,r.jsxs)(n.p,{children:["\u51fa\u4e8e\u6d4f\u89c8\u5668\u7684\u540c\u6e90\u7b56\u7565\u9650\u5236\u3002\u540c\u6e90\u7b56\u7565\uff08Sameoriginpolicy\uff09\u662f\u4e00\u79cd\u7ea6\u5b9a\uff0c\u5b83\u662f\u6d4f\u89c8\u5668\u6700\u6838\u5fc3\u4e5f\u6700\u57fa\u672c\u7684\u5b89\u5168\u529f\u80fd\uff0c\u5982\u679c\u7f3a\u5c11\u4e86\u540c\u6e90\u7b56\u7565\uff0c\u5219\u6d4f\u89c8\u5668\u7684\u6b63\u5e38\u529f\u80fd\u53ef\u80fd\u90fd\u4f1a\u53d7\u5230\u5f71\u54cd\u3002\u53ef\u4ee5\u8bf4 ",(0,r.jsx)(n.code,{children:"Web"})," \u662f\u6784\u5efa\u5728\u540c\u6e90\u7b56\u7565\u57fa\u7840\u4e4b\u4e0a\u7684\uff0c\u6d4f\u89c8\u5668\u53ea\u662f\u9488\u5bf9\u540c\u6e90\u7b56\u7565\u7684\u4e00\u79cd\u5b9e\u73b0\u3002\u540c\u6e90\u7b56\u7565\u4f1a\u963b\u6b62\u4e00\u4e2a\u57df\u7684 javascript \u811a\u672c\u548c\u53e6\u5916\u4e00\u4e2a\u57df\u7684\u5185\u5bb9\u8fdb\u884c\u4ea4\u4e92\u3002\u6240\u8c13\u540c\u6e90\uff08\u5373\u6307\u5728\u540c\u4e00\u4e2a\u57df\uff09\u5c31\u662f\u4e24\u4e2a\u9875\u9762\u5177\u6709\u76f8\u540c\u7684\u534f\u8bae\uff08protocol\uff09\uff0c\u4e3b\u673a\uff08host\uff09\u548c\u7aef\u53e3\u53f7\uff08port\uff09\u3002"]}),"\n",(0,r.jsx)(n.h2,{id:"162-\u6709\u8de8\u57df\u884c\u4e3a\u793a\u4f8b",children:"16.2 \u6709\u8de8\u57df\u884c\u4e3a\u793a\u4f8b"}),"\n",(0,r.jsxs)(n.table,{children:[(0,r.jsx)(n.thead,{children:(0,r.jsxs)(n.tr,{children:[(0,r.jsx)(n.th,{children:"\u5f53\u524d\u9875\u9762 url"}),(0,r.jsx)(n.th,{children:"\u88ab\u8bf7\u6c42\u9875\u9762 url"}),(0,r.jsx)(n.th,{children:"\u662f\u5426\u8de8\u57df"}),(0,r.jsx)(n.th,{children:"\u539f\u56e0"})]})}),(0,r.jsxs)(n.tbody,{children:[(0,r.jsxs)(n.tr,{children:[(0,r.jsx)(n.td,{children:(0,r.jsx)(n.a,{href:"http://www.baiqian.ltd/",children:"http://www.baiqian.ltd/"})}),(0,r.jsx)(n.td,{children:(0,r.jsx)(n.a,{href:"http://www.baiqian.ltd/index.html",children:"http://www.baiqian.ltd/index.html"})}),(0,r.jsx)(n.td,{children:"\u5426"}),(0,r.jsx)(n.td,{children:"\u540c\u6e90\uff08\u534f\u8bae\u3001\u57df\u540d\u3001\u7aef\u53e3\u53f7\u76f8\u540c\uff09"})]}),(0,r.jsxs)(n.tr,{children:[(0,r.jsx)(n.td,{children:(0,r.jsx)(n.a,{href:"http://www.baiqian.ltd/",children:"http://www.baiqian.ltd/"})}),(0,r.jsx)(n.td,{children:(0,r.jsx)(n.a,{href:"https://www.baiqian.ltd/index.html",children:"https://www.baiqian.ltd/index.html"})}),(0,r.jsx)(n.td,{children:"\u8de8\u57df"}),(0,r.jsx)(n.td,{children:"\u534f\u8bae\u4e0d\u540c\uff08http/https\uff09"})]}),(0,r.jsxs)(n.tr,{children:[(0,r.jsx)(n.td,{children:(0,r.jsx)(n.a,{href:"http://www.baiqian.ltd/",children:"http://www.baiqian.ltd/"})}),(0,r.jsx)(n.td,{children:(0,r.jsx)(n.a,{href:"http://www.baidu.com/",children:"http://www.baidu.com/"})}),(0,r.jsx)(n.td,{children:"\u8de8\u57df"}),(0,r.jsx)(n.td,{children:"\u4e3b\u57df\u540d\u4e0d\u540c\uff08baiqian.ltd/baidu.com\uff09"})]}),(0,r.jsxs)(n.tr,{children:[(0,r.jsx)(n.td,{children:(0,r.jsx)(n.a,{href:"http://furion.baiqian.ltd/",children:"http://furion.baiqian.ltd/"})}),(0,r.jsx)(n.td,{children:(0,r.jsx)(n.a,{href:"http://fur.baiqian.ltd/",children:"http://fur.baiqian.ltd/"})}),(0,r.jsx)(n.td,{children:"\u8de8\u57df"}),(0,r.jsx)(n.td,{children:"\u5b50\u57df\u540d\u4e0d\u540c\uff08furion/fur\uff09"})]}),(0,r.jsxs)(n.tr,{children:[(0,r.jsx)(n.td,{children:(0,r.jsx)(n.a,{href:"http://www.baiqian.ltd:8080/",children:"http://www.baiqian.ltd:8080/"})}),(0,r.jsx)(n.td,{children:(0,r.jsx)(n.a,{href:"http://www.baiqian.ltd:7001/",children:"http://www.baiqian.ltd:7001/"})}),(0,r.jsx)(n.td,{children:"\u8de8\u57df"}),(0,r.jsx)(n.td,{children:"\u7aef\u53e3\u53f7\u4e0d\u540c\uff088080/7001\uff09"})]})]})]}),"\n",(0,r.jsx)(n.h2,{id:"163-\u4ec0\u4e48\u662f-cors",children:"16.3 \u4ec0\u4e48\u662f CORS"}),"\n",(0,r.jsxs)(n.p,{children:["\u8de8\u6e90\u8d44\u6e90\u5171\u4eab (",(0,r.jsx)(n.code,{children:"CORS"}),") \uff1a"]}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsxs)(n.li,{children:["\u662f\u4e00\u79cd ",(0,r.jsx)(n.code,{children:"W3C"})," \u6807\u51c6\uff0c\u53ef\u8ba9\u670d\u52a1\u5668\u653e\u5bbd\u76f8\u540c\u7684\u6e90\u7b56\u7565\u3002"]}),"\n",(0,r.jsxs)(n.li,{children:["\u4e0d\u662f\u4e00\u9879\u5b89\u5168\u529f\u80fd\uff0c",(0,r.jsx)(n.code,{children:"CORS"})," \u653e\u5bbd ",(0,r.jsx)(n.code,{children:"security"}),"\u3002 ",(0,r.jsx)(n.code,{children:"API"})," \u4e0d\u80fd\u901a\u8fc7\u5141\u8bb8 ",(0,r.jsx)(n.code,{children:"CORS"})," \u6765\u66f4\u5b89\u5168\u3002 \u6709\u5173\u8be6\u7ec6\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605 ",(0,r.jsx)(n.a,{href:"https://docs.microsoft.com/zh-cn/aspnet/core/security/cors?view=aspnetcore-5.0#how-cors",children:"CORS \u5de5\u4f5c\u539f\u7406"}),"\u3002"]}),"\n",(0,r.jsx)(n.li,{children:"\u5141\u8bb8\u670d\u52a1\u5668\u660e\u786e\u5141\u8bb8\u4e00\u4e9b\u8de8\u6e90\u8bf7\u6c42\uff0c\u540c\u65f6\u62d2\u7edd\u5176\u4ed6\u8bf7\u6c42\u3002"}),"\n",(0,r.jsxs)(n.li,{children:["\u6bd4\u65e9\u671f\u7684\u6280\u672f\uff08\u5982 ",(0,r.jsx)(n.code,{children:"JSONP"}),"\uff09\u66f4\u5b89\u5168\u4e14\u66f4\u7075\u6d3b\u3002"]}),"\n"]}),"\n",(0,r.jsx)(n.h2,{id:"164-\u5982\u4f55\u4f7f\u7528",children:"16.4 \u5982\u4f55\u4f7f\u7528"}),"\n",(0,r.jsxs)(n.h3,{id:"1641-\u6dfb\u52a0-cors-\u670d\u52a1",children:["16.4.1 \u6dfb\u52a0 ",(0,r.jsx)(n.code,{children:"CORS"})," \u670d\u52a1"]}),"\n",(0,r.jsxs)(n.p,{children:["\u542f\u7528\u8de8\u57df ",(0,r.jsx)(n.code,{children:"Cors"})," \u652f\u6301\u9996\u5148\u6dfb\u52a0 ",(0,r.jsx)(n.code,{children:"CorsAccessor"})," \u670d\u52a1\uff0c\u5982\uff1a"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {13,22}",children:"using Microsoft.AspNetCore.Builder;\r\nusing Microsoft.AspNetCore.Hosting;\r\nusing Microsoft.Extensions.DependencyInjection;\r\nusing Microsoft.Extensions.Hosting;\r\n\r\nnamespace Furion.Web.Core\r\n{\r\n [AppStartup(700)]\r\n public sealed class FurWebCoreStartup : AppStartup\r\n {\r\n public void ConfigureServices(IServiceCollection services)\r\n {\r\n services.AddCorsAccessor();\r\n\r\n // ...\r\n }\r\n\r\n public void Configure(IApplicationBuilder app, IWebHostEnvironment env)\r\n {\r\n //...\r\n\r\n app.UseCorsAccessor();\r\n\r\n // ...\r\n }\r\n }\r\n}\n"})}),"\n",(0,r.jsxs)(n.admonition,{title:"\u7279\u522b\u6ce8\u610f",type:"caution",children:[(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"services.AddCorsAccessor();"})," \u9700\u5728 ",(0,r.jsx)(n.code,{children:"services.AddControllers()"})," \u4e4b\u524d\u6ce8\u518c\u3002"]}),(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"app.UseCorsAccessor();"})," \u9700\u5728 ",(0,r.jsx)(n.code,{children:"app.UseRouting();"})," \u548c ",(0,r.jsx)(n.code,{children:"app.UseAuthentication();"})," \u4e4b\u95f4\u6ce8\u518c\u3002"]})]}),"\n",(0,r.jsx)(n.h3,{id:"1642-\u914d\u7f6e\u5141\u8bb8\u8de8\u57df\u57df\u540d",children:"16.4.2 \u914d\u7f6e\u5141\u8bb8\u8de8\u57df\u57df\u540d"}),"\n",(0,r.jsx)(n.admonition,{title:"\u5c0f\u63d0\u9192",type:"important",children:(0,r.jsxs)(n.p,{children:["\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c",(0,r.jsx)(n.code,{children:"Furion"})," \u5141\u8bb8\u6240\u6709\u57df\u540d\u6765\u6e90\u8bbf\u95ee\uff0c\u4e5f\u5c31\u662f\u65e0\u9700\u914d\u7f6e\u4efb\u4f55\u6765\u6e90\u57df\u540d\uff0c\u53e6\u5916\u524d\u7aef\u4e5f\u9700\u8981\u8bbe\u7f6e\u8bf7\u6c42\u53c2\u6570\uff1a",(0,r.jsx)(n.code,{children:"withCredentials:false"})]})}),"\n",(0,r.jsx)(n.p,{children:"\u5982\u679c\u9700\u8981\u6307\u5b9a\u7279\u5b9a\u57df\u540d\uff0c\u5219\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\u5373\u53ef\uff1a"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-json",metastring:"showLineNumbers {4}",children:'{\r\n "CorsAccessorSettings": {\r\n "PolicyName": "\u81ea\u5b9a\u4e49\u8de8\u57df\u7b56\u7565\u540d",\r\n "WithOrigins": ["http://localhost:4200", "http://furion.baiqian.ltd"]\r\n }\r\n}\n'})}),"\n",(0,r.jsxs)(n.h2,{id:"165-corsaccessorsettings-\u914d\u7f6e",children:["16.5 ",(0,r.jsx)(n.code,{children:"CorsAccessorSettings"})," \u914d\u7f6e"]}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsxs)(n.li,{children:[(0,r.jsx)(n.code,{children:"CorsAccessorSettings"}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsxs)(n.li,{children:[(0,r.jsx)(n.code,{children:"PolicyName"}),"\uff1a\u8de8\u57df\u7b56\u7565\u540d\uff0c",(0,r.jsx)(n.code,{children:"string"})," \u7c7b\u578b\uff0c\u5fc5\u586b\uff0c\u9ed8\u8ba4 ",(0,r.jsx)(n.code,{children:"App.Cors.Policy"})]}),"\n",(0,r.jsxs)(n.li,{children:[(0,r.jsx)(n.code,{children:"WithOrigins"}),"\uff1a\u5141\u8bb8\u8de8\u57df\u7684\u57df\u540d\u5217\u8868\uff0c",(0,r.jsx)(n.code,{children:"string[]"})," \u7c7b\u578b\uff0c\u9ed8\u8ba4 ",(0,r.jsx)(n.code,{children:"*"})]}),"\n",(0,r.jsxs)(n.li,{children:[(0,r.jsx)(n.code,{children:"WithHeaders"}),"\uff1a\u8bf7\u6c42\u8868\u5934\uff0c\u6ca1\u6709\u914d\u7f6e\u5219\u5141\u8bb8\u6240\u6709\u8868\u5934\uff0c",(0,r.jsx)(n.code,{children:"string[]"})," \u7c7b\u578b"]}),"\n",(0,r.jsxs)(n.li,{children:[(0,r.jsxs)(n.strong,{children:[(0,r.jsx)(n.code,{children:"WithExposedHeaders"}),"\uff1a\u8bbe\u7f6e\u5ba2\u6237\u7aef\u53ef\u83b7\u53d6\u7684\u54cd\u5e94\u6807\u5934\uff0c",(0,r.jsx)(n.code,{children:"string[]"})," \u7c7b\u578b\uff0c\u9ed8\u8ba4 ",(0,r.jsx)(n.code,{children:'["access-token", "x-access-token"]'})]}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsx)(n.li,{children:(0,r.jsxs)(n.strong,{children:["\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u82e5\u540e\u7aef\u8f93\u51fa\u7279\u5b9a\u7684\u54cd\u5e94\u5934 ",(0,r.jsx)(n.code,{children:"Key"}),"\uff0c\u90a3\u4e48\u9700\u5c06\u8be5 ",(0,r.jsx)(n.code,{children:"Key"})," \u914d\u7f6e\u5728\u6570\u7ec4\u4e2d"]})}),"\n"]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:[(0,r.jsx)(n.code,{children:"WithMethods"}),"\uff1a\u8bbe\u7f6e\u8de8\u57df\u5141\u8bb8\u8bf7\u6c42\u8c13\u8bcd\uff0c\u6ca1\u6709\u914d\u7f6e\u5219\u5141\u8bb8\u6240\u6709\uff0c",(0,r.jsx)(n.code,{children:"string[]"})," \u7c7b\u578b"]}),"\n",(0,r.jsxs)(n.li,{children:[(0,r.jsx)(n.code,{children:"AllowCredentials"}),"\uff1a\u662f\u5426\u5141\u8bb8\u8de8\u57df\u8bf7\u6c42\u4e2d\u7684\u51ed\u636e\uff0c",(0,r.jsx)(n.code,{children:"bool"})," \u7c7b\u578b\uff0c\u9ed8\u8ba4\u503c ",(0,r.jsx)(n.code,{children:"true"})]}),"\n",(0,r.jsxs)(n.li,{children:[(0,r.jsx)(n.code,{children:"SetPreflightMaxAge"}),"\uff1a\u8bbe\u7f6e\u9884\u68c0\u8fc7\u671f\u65f6\u95f4\uff0c",(0,r.jsx)(n.code,{children:"int"})," \u7c7b\u578b\uff0c\u9ed8\u8ba4\u503c ",(0,r.jsx)(n.code,{children:"24\u5c0f\u65f6"})]}),"\n",(0,r.jsxs)(n.li,{children:[(0,r.jsx)(n.code,{children:"FixedClientToken"}),"\uff1a\u662f\u5426\u9ed8\u8ba4\u914d\u7f6e ",(0,r.jsx)(n.code,{children:"WithExposedHeaders"}),"\uff0c",(0,r.jsx)(n.code,{children:"bool"})," \u7c7b\u578b\uff0c\u9ed8\u8ba4 ",(0,r.jsx)(n.code,{children:"true"})]}),"\n",(0,r.jsxs)(n.li,{children:[(0,r.jsx)(n.code,{children:"SignalRSupport"}),"\uff1a\u662f\u5426\u542f\u7528 ",(0,r.jsx)(n.code,{children:"SignalR"})," \u8de8\u57df\u652f\u6301\uff0c",(0,r.jsx)(n.code,{children:"bool"})," \u7c7b\u578b\uff0c\u9ed8\u8ba4 ",(0,r.jsx)(n.code,{children:"false"})]}),"\n"]}),"\n"]}),"\n"]}),"\n",(0,r.jsx)(n.h2,{id:"166-\u524d\u7aef\u4e0d\u80fd\u8bfb\u53d6\u54cd\u5e94\u5934\u6ce8\u610f\u4e8b\u9879",children:"16.6 \u524d\u7aef\u4e0d\u80fd\u8bfb\u53d6\u54cd\u5e94\u5934\u6ce8\u610f\u4e8b\u9879"}),"\n",(0,r.jsxs)(n.p,{children:["\u6709\u65f6\u5019\uff0c\u6211\u4eec\u901a\u8fc7 ",(0,r.jsx)(n.code,{children:"ajax"})," \u6216\u8005 ",(0,r.jsx)(n.code,{children:"axios"})," \u7b2c\u4e09\u65b9\u5e93\u65e0\u6cd5\u8bfb\u53d6\u54cd\u5e94\u5934\u81ea\u5b9a\u4e49\u4fe1\u606f\uff0c\u8fd9\u65f6\u9700\u8981\u54cd\u5e94\u62a5\u6587\u4e2d\u516c\u5f00\u7279\u5b9a ",(0,r.jsx)(n.code,{children:"Header"})," \u624d\u80fd\u653e\u884c\uff0c\u5982\uff1a",(0,r.jsx)(n.code,{children:"Access-Control-Expose-Headers: xxxxx"}),"\uff0c\u6240\u4ee5\uff0c\u9700\u8981\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-cs",metastring:'showLineNumbers title="appsettings.json"',children:'{\r\n "CorsAccessorSettings": {\r\n "WithExposedHeaders": ["access-token","x-access-token"]\r\n }\r\n}\n'})}),"\n",(0,r.jsxs)(n.p,{children:["\u9700\u8981\u83b7\u53d6\u54ea\u4e2a\u5934\uff0c\u5c31\u5728 ",(0,r.jsx)(n.code,{children:"WithExposedHeaders"})," \u6570\u7ec4\u4e2d\u914d\u7f6e\u5373\u53ef\u3002\u5982\u679c\u4f7f\u7528 ",(0,r.jsx)(n.code,{children:"ajax"})," \u53ef\u4ee5\u901a\u8fc7 ",(0,r.jsx)(n.code,{children:"xhr.getResponseHeader(key)"})," \u6216 ",(0,r.jsx)(n.code,{children:"xhr.getAllResponseHeaders()"})," \u83b7\u53d6\u914d\u7f6e\u7684 ",(0,r.jsx)(n.code,{children:"key"}),"\u3002"]}),"\n",(0,r.jsxs)(n.p,{children:["\u7279\u522b\u60c5\u51b5\u4e0b\u4e0d\u80fd\u8bf7\u6c42\uff0c\u53ef\u4ee5\u8003\u8651\u8bbe\u7f6e ",(0,r.jsx)(n.code,{children:"withCredentials: false"})," \u3002"]}),"\n",(0,r.jsxs)(n.h2,{id:"167-\u4f7f\u7528-ajax-\u524d\u7aef\u6ce8\u610f\u4e8b\u9879",children:["16.7 \u4f7f\u7528 ",(0,r.jsx)(n.code,{children:"$.ajax"})," \u524d\u7aef\u6ce8\u610f\u4e8b\u9879"]}),"\n",(0,r.jsxs)(n.p,{children:["\u4f7f\u7528 ",(0,r.jsx)(n.code,{children:"Jquery"})," \u524d\u7aef\u8bf7\u6c42\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u914d\u7f6e\uff1a"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {4-7}",children:'$.ajax({\r\n \xa0 \xa0 \xa0 \xa0url: "https://localhost:5001/api/system/getdata",\r\n \xa0 \xa0 \xa0 \xa0type: "GET",\r\n \xa0 \xa0 \xa0 \xa0xhrFields: {\r\n \xa0 \xa0 \xa0 \xa0\xa0 \xa0 withCredentials: false // \u5982\u679c\u662fhttps\u8bf7\u6c42\uff0c\u53ef\u4ee5\u8bd5\u8bd5 true\r\n \xa0 \xa0 \xa0 \xa0},\r\n \xa0 \xa0 \xa0 \xa0crossDomain: true,\r\n \xa0 \xa0 \xa0 \xa0success: function (res) {\r\n \xa0 \xa0 \xa0 \xa0\xa0 \xa0 render(res);\r\n \xa0 \xa0 \xa0 \xa0}\r\n});\n'})}),"\n",(0,r.jsxs)(n.admonition,{title:"\u7279\u522b\u6ce8\u610f",type:"important",children:[(0,r.jsxs)(n.p,{children:["\u5728\u672c\u5730\u5f00\u53d1\u9636\u6bb5\uff0c\u8bf7\u6c42\u5982\u679c\u51fa\u73b0 ",(0,r.jsx)(n.code,{children:" Access to XMLHttpRequest...has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header..."})," \u9519\u8bef\uff0c\u8bf7\u786e\u4fdd ",(0,r.jsx)(n.code,{children:"ajax"})," \u7684 ",(0,r.jsx)(n.code,{children:"url"})," \u53c2\u6570\u662f\u6b63\u786e\u7684\uff0c\u901a\u5e38",(0,r.jsx)(n.strong,{children:"\u9519\u8bef\u7684\u505a\u6cd5"}),"\u662f\uff1a"]}),(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsxs)(n.li,{children:["\u4f7f\u7528\u4e86 ",(0,r.jsx)(n.code,{children:"127.0.0.1"})," \u800c\u4e0d\u662f ",(0,r.jsx)(n.code,{children:"localhost"})," \u4e3b\u673a\u5730\u5740"]}),"\n",(0,r.jsxs)(n.li,{children:["\u4f7f\u7528 ",(0,r.jsx)(n.code,{children:"http"})," \u800c\u4e0d\u662f ",(0,r.jsx)(n.code,{children:"https"})," \u4e3b\u673a\u534f\u8bae"]}),"\n",(0,r.jsxs)(n.li,{children:["\u4f7f\u7528\u4e86 ",(0,r.jsx)(n.code,{children:"5000"})," \u800c\u4e0d\u662f ",(0,r.jsx)(n.code,{children:"5001"})," \u4e3b\u673a\u7aef\u53e3"]}),"\n"]})]}),"\n",(0,r.jsx)(n.h2,{id:"168-\u7981\u7528\u8de8\u57df",children:"16.8 \u7981\u7528\u8de8\u57df"}),"\n",(0,r.jsxs)(n.p,{children:["\u6709\u65f6\u5019\uff0c\u6211\u4eec\u5e0c\u671b\u67d0\u4e2a\u65b9\u6cd5\u4e0d\u68c0\u67e5\u8de8\u57df\u8bf7\u6c42\uff0c\u53ef\u4ee5\u5728 ",(0,r.jsx)(n.code,{children:"Action"})," \u4e2d\u8d34 ",(0,r.jsx)(n.code,{children:"[DisableCors]"})," \u7279\u6027\u5373\u53ef\u3002"]}),"\n",(0,r.jsxs)(n.h2,{id:"169-signalr-\u8de8\u57df\u95ee\u9898",children:["16.9 ",(0,r.jsx)(n.code,{children:"SignalR"})," \u8de8\u57df\u95ee\u9898"]}),"\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"SignalR"})," \u5b9e\u73b0\u8de8\u57df\u9700\u8981\u6ee1\u8db3\u4e0b\u9762\u51e0\u4e2a\u6761\u4ef6\uff1a"]}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsx)(n.li,{children:"\u5141\u8bb8\u7279\u5b9a\u7684\u9884\u671f\u6765\u6e90\uff0c\u5141\u8bb8\u4efb\u4f55\u6765\u6e90\u662f\u53ef\u884c\u7684\uff0c\u4f46\u4e0d\u5b89\u5168\u6216\u4e0d\u63a8\u8350\u4f7f\u7528"}),"\n",(0,r.jsxs)(n.li,{children:["\u5fc5\u987b\u5141\u8bb8\u4f7f\u7528 HTTP \u65b9\u6cd5 ",(0,r.jsx)(n.code,{children:"GET"})," \u548c ",(0,r.jsx)(n.code,{children:"POST"})]}),"\n",(0,r.jsxs)(n.li,{children:["\u4e3a\u4e86\u4f7f\u57fa\u4e8e ",(0,r.jsx)(n.code,{children:"cookie"})," \u7684\u7c98\u6ede\u4f1a\u8bdd\u6b63\u5e38\u5de5\u4f5c\uff0c\u5fc5\u987b\u5141\u8bb8\u4f7f\u7528\u51ed\u636e\uff0c\u5373\u4f7f\u672a\u4f7f\u7528\u8eab\u4efd\u9a8c\u8bc1\uff0c\u4e5f\u5fc5\u987b\u542f\u7528\u5b83\u4eec\u3002"]}),"\n"]}),"\n",(0,r.jsxs)(n.p,{children:["\u5b98\u65b9\u6587\u6863\u8bf4\u660e ",(0,r.jsx)(n.a,{href:"https://docs.microsoft.com/zh-cn/aspnet/core/signalr/security?view=aspnetcore-6.0",children:"https://docs.microsoft.com/zh-cn/aspnet/core/signalr/security?view=aspnetcore-6.0"})]}),"\n",(0,r.jsxs)(n.p,{children:[(0,r.jsxs)(n.strong,{children:["\u5728 ",(0,r.jsx)(n.code,{children:"Furion 4.1.4+"})," \u7248\u672c\u5df2\u4fee\u6b63 ",(0,r.jsx)(n.code,{children:"SignalR"})," \u8de8\u57df\u95ee\u9898\uff0c\u53ea\u9700\u8981\u542f\u7528 ",(0,r.jsx)(n.code,{children:"SignalRSupport"})," \u914d\u7f6e\u5373\u53ef"]}),"\uff0c\u5982\uff1a"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-json",metastring:"showLineNumbers {2,3}",children:'{\r\n "CorsAccessorSettings": {\r\n "SignalRSupport": true\r\n }\r\n}\n'})}),"\n",(0,r.jsx)(n.h2,{id:"1610-\u9759\u6001\u8d44\u6e90\u8de8\u57df\u95ee\u9898",children:"16.10 \u9759\u6001\u8d44\u6e90\u8de8\u57df\u95ee\u9898"}),"\n",(0,r.jsxs)(n.p,{children:["\u6709\u65f6\u5019\u6211\u4eec\u53ef\u80fd\u901a\u8fc7\u524d\u7aef ",(0,r.jsx)(n.code,{children:"XMLHttpRequest/Ajax/Fetch"})," \u65b9\u5f0f\u52a0\u8f7d\u9759\u6001\u8d44\u6e90\uff0c\u8fd9\u65f6\u53ef\u80fd\u51fa\u73b0\u8de8\u57df\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u914d\u7f6e\u89e3\u51b3\uff1a"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,3,5-7}",children:'app.UseStaticFiles(new StaticFileOptions\r\n{\r\n OnPrepareResponse = (stf) =>\r\n {\r\n stf.Context.Response.Headers["Access-Control-Allow-Origin"] = "*";\r\n stf.Context.Response.Headers["Access-Control-Allow-Headers"] = "*";\r\n }\r\n});\n'})}),"\n",(0,r.jsx)(n.admonition,{title:"\u5c0f\u77e5\u8bc6",type:"tip",children:(0,r.jsxs)(n.p,{children:["\u5982\u679c\u5df2\u7ecf\u6ce8\u518c\u4e86 ",(0,r.jsx)(n.code,{children:"app.UseStaticFiles()"}),"\uff0c\u5219\u53ea\u9700\u8981\u4f20\u9012 ",(0,r.jsx)(n.code,{children:"new StaticFileOptions{ ... }"})," \u53c2\u6570\u5373\u53ef\uff0c\u907f\u514d\u591a\u6b21\u6ce8\u518c ",(0,r.jsx)(n.code,{children:"app.UseStaticFiles()"}),"\u3002"]})}),"\n",(0,r.jsx)(n.h2,{id:"1611-\u53cd\u9988\u4e0e\u5efa\u8bae",children:"16.11 \u53cd\u9988\u4e0e\u5efa\u8bae"}),"\n",(0,r.jsx)(n.admonition,{title:"\u4e0e\u6211\u4eec\u4ea4\u6d41",type:"note",children:(0,r.jsxs)(n.p,{children:["\u7ed9 Furion \u63d0 ",(0,r.jsx)(n.a,{href:"https://gitee.com/dotnetchina/Furion/issues/new?issue",children:"Issue"}),"\u3002"]})}),"\n",(0,r.jsx)(n.hr,{}),"\n",(0,r.jsx)(n.admonition,{title:"\u4e86\u89e3\u66f4\u591a",type:"note",children:(0,r.jsxs)(n.p,{children:["\u60f3\u4e86\u89e3\u66f4\u591a ",(0,r.jsx)(n.code,{children:"\u8de8\u57df\u8bf7\u6c42"})," \u77e5\u8bc6\u53ef\u67e5\u9605 ",(0,r.jsx)(n.a,{href:"https://docs.microsoft.com/zh-cn/aspnet/core/security/cors?view=aspnetcore-5.0",children:"ASP.NET Core - \u542f\u7528\u8de8\u57df\u8bf7\u6c42"})," \u7ae0\u8282\u3002"]})})]})}function a(e={}){const{wrapper:n}={...(0,i.a)(),...e.components};return n?(0,r.jsx)(n,{...e,children:(0,r.jsx)(h,{...e})}):h(e)}},1151:(e,n,s)=>{s.d(n,{Z:()=>l,a:()=>d});var r=s(7294);const i={},c=r.createContext(i);function d(e){const n=r.useContext(c);return r.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function l(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:d(e.components),r.createElement(c.Provider,{value:n},e.children)}}}]);