简体   繁体   English

在Asp.net Core 2.1的Angular 6中不存在“ Access-Control-Allow-Origin”标头

[英]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;
            });

    }

错误详情 HttpCLient HttpClient的

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.

相关问题 ASP.Net核心WebAPI - 请求的资源上没有“Access-Control-Allow-Origin”标头 - ASP.Net Core WebAPI - No 'Access-Control-Allow-Origin' header is present on the requested resource Angular 4 / Asp.net Web API-不存在“ Access-Control-Allow-Origin”标头 - Angular 4 / Asp.net Web API - No 'Access-Control-Allow-Origin' header is present 不存在“Access-Control-Allow-Origin”标头 - 面向 .net 框架 4.5.1 的 asp.net 核心 Web api - No 'Access-Control-Allow-Origin' header is present - asp.net core web api targeting .net framework 4.5.1 React+ASP.NET.Core:请求的资源上不存在“Access-Control-Allow-Origin”标头 - React+ASP.NET.Core : No 'Access-Control-Allow-Origin' header is present on the requested resource ASP.NET Web窗体:所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - ASP.NET Web Forms: No 'Access-Control-Allow-Origin' header is present on the requested resource ASP.NET Core 6 中的“访问控制允许来源” - 'Access-Control-Allow-Origin' in ASP.NET Core 6 In.Net Core 2.1 Web API 用于 PUT 和 DELETE,仅为什么请求的资源上存在“No 'Access-Control-Allow-Origin' header” - In .Net Core 2.1 Web API for PUT and DELETE only why “No 'Access-Control-Allow-Origin' header is present on the requested resource” ASP.NET Core CORS WebAPI:没有 Access-Control-Allow-Origin 标头 - ASP.NET Core CORS WebAPI: no Access-Control-Allow-Origin header CORS asp.net 核心 webapi - 缺少 Access-Control-Allow-Origin 标头 - CORS asp.net core webapi - missing Access-Control-Allow-Origin header ASP.NET Core CORS WebAPI:不保留 Access-Control-Allow-Origin 标头 - ASP.NET Core CORS WebAPI: persist no Access-Control-Allow-Origin header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM