Moh*_*iri 0 asp.net-core angular
在我的Asp.net核心项目中,我使用Microsoft.AspNetCore.App Version =“ 2.2.1”并在启动服务中调用AddCors
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
);
});
.....
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors(policyName: "CorsPolicy");
....
Run Code Online (Sandbox Code Playgroud)
Controller :
[Route("api/[controller]")]
[EnableCors("CorsPolicy")]
public class ManageUsersController : Controller
{ ...
Run Code Online (Sandbox Code Playgroud)
在angular 5 App中,当调用Web API时,在浏览器的控制台中显示此错误
CORS策略已阻止从原点' http:// localhost:4200 ' 访问' http:// localhost:5000 / api / Account / LoginByPortal ' 上的XMLHttpRequest :对预检请求的响应未通过访问控制检查:当请求的凭据模式为“ include”时,响应中“ Access-Control-Allow-Origin”标头的值不得为通配符“ *”。XMLHttpRequest发起的请求的凭据模式由withCredentials属性控制。
角度服务:
loginByPortal(credentials: Credentials): Observable<userLoginViewmodel> {
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
return this.http
.post(`${this.appConfig.apiEndpoint}/Account/LoginByPortal`, credentials,
{ headers: headers, withCredentials: true /* For CORS */ })
.map(response => response || {})
.catch((error: HttpErrorResponse) => Observable.throw(error));
}
Run Code Online (Sandbox Code Playgroud)
我不想使用.WithOrigins("http://localhost:4200"
我想在CoresPloicy中使用AllowAnyOrigin有
什么问题?我该如何解决?
这是解决方案:https : //stackoverflow.com/a/53790841/8801075
app.UseCors(builder => builder
.AllowAnyHeader()
.AllowAnyMethod()
.SetIsOriginAllowed((host) => true)
.AllowCredentials()
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1122 次 |
| 最近记录: |