[英]No 'Access-Control-Allow-Origin' header is present Angular 6 with Asp.net Core 2.1
Keep getting No 'Access-Control-Allow-Origin' header is present error in angular 6 with asp.net core 2.1. 不断获取没有 asp.net core 2.1在angular 6中出现“ Access-Control-Allow-Origin”标头错误。
I know this is the cors errors. 我知道这是cors错误。 I, have added the policy in the server side but still same issue.
我在服务器端添加了策略,但仍然存在相同的问题。
startup.cs startup.cs
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials().WithOrigins("http://localhost:4200");
}));
app.UseCors("CorsPolicy");
app.UseMvc();
Controller Code 控制器代码
[HttpPost, AllowAnonymous]
public GenericResponseObject<JwtTokenViewModel> UserAuthentication([FromBody] UserViewModel user)
{
GenericResponseObject<JwtTokenViewModel> genericResponseObject = new GenericResponseObject<JwtTokenViewModel>();
genericResponseObject.IsSuccess = false;
genericResponseObject.Message = ConstaintStingValue.TagConnectionFailed;
if (ModelState.IsValid)
{
try
{
UserViewModel userViewModel = _adminUOW.AdminAuthentication(user);
if (userViewModel != null)
{
string token = new CommonController(tokenOptions).GetToken(userViewModel, DateTime.UtcNow.AddDays(2));
genericResponseObject.Data = new JwtTokenViewModel
{
UserName = userViewModel.UserName,
UserId = userViewModel.UserId,
authenticated = true,
entityId = 1,
tokenExpires = DateTime.UtcNow.AddHours(2),
token = token,
SelectedRole = userViewModel.SelectedRole
};
}
else
{
genericResponseObject.SuccessMessage = ConstaintStingValue.Tag_No_Profile_key;
}
genericResponseObject.IsSuccess = true;
genericResponseObject.Message = ConstaintStingValue.TagConnectionSuccess;
}
catch (Exception exception)
{
genericResponseObject.IsSuccess = true;
genericResponseObject.SuccessMessage = exception.Message;
genericResponseObject.ErrorCode = exception.HResult;
genericResponseObject.ExceptionErrorMessage = exception.StackTrace;
WriteExceptionToFile.WriteExceptionToFileInLogger(_hostingEnvironment, exception);
}
}
return genericResponseObject;
}
Typescript code 打字稿代码
login(modelValue: UserViewModel, isValid: boolean) {
if (isValid) { this.genericHttpClientService.GenericHttpPostAndResponse<GenericResponseObject<JwtTokenViewModel>, UserViewModel>(this._userViewModel, this.commonConstantValue.adminLoginUrl).subscribe(item => {
if (item.data!= null && item.data.authenticated) {
this.router.navigate(['admin-portal']);
}else{
this.genericViewModel.successMessage = item.successMessage;
}
this.progressBarDisplay = false;
},
err => {
console.log(err);
this.progressBarDisplay = false;
});
}
public GenericHttpPostAndResponse<T,TE>(postViewModel: TE, destinationUrl: string): Observable<T> {
const headers = new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8');
return this.httpClientModule.post<T>(environment.baseUrl + destinationUrl, postViewModel, { headers });
}
Getting Error " No 'Access-Control-Allow-Origin' header is present on the requested resource." 错误:“请求的资源上没有'Access-Control-Allow-Origin'标头。” in ionic2
在ionic2中
How to enable CORS in ASP.net Core WebAPI 如何在ASP.net Core WebAPI中启用CORS
https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-2.1 https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-2.1
Hard to say what the real problem is. 很难说真正的问题是什么。 My experience with CORS is that it is harder in practice than one would think.
我在CORS上的经验是,实践中比想象的要难。 I had a similar problem, which was due to my site requiring Windows Authentication.
我有一个类似的问题,这是由于我的站点需要Windows身份验证。 Turns out that the preflight CORS request requires anonymous authentication.
事实证明,飞行前CORS请求需要匿名身份验证。 I see that your controller has AllowAnonymous attribute, but, if this is hosted in ie IIS, then the site needs to have anonymous authenticationt too.
我看到您的控制器具有AllowAnonymous属性,但是,如果该属性托管在IIS中,则该站点也需要具有匿名身份验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.