Issue
I have had this issue for some time now and I have tried every possible solution for it, my asp.net core server has CORS enabled, code as follow, but sometimes I get this error of No 'Access-Control-Allow-Origin'
My Production API is hosted on IIS on windows server
Is there any way i can get this debugged?
Access to XMLHttpRequest at 'Production_API' from origin 'DOMAIN' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. zone.js:3372
GET APICALL net::ERR_FAILED
in zone.js this is where it says it has a error "Failed to load resource: net::ERR_FAILED"
sendNative.apply(target, data.args);
target[XHR_SCHEDULED] = true;
return task;
ASP.net Core CORS Code
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowEverything",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>(config =>
{
config.SignIn.RequireConfirmedEmail = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddDefaultUI()
.AddDefaultTokenProviders();
services.AddAuthentication(
option =>
{
option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
option.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters.ValidateIssuer = true;
options.TokenValidationParameters.ValidateIssuerSigningKey = true;
options.TokenValidationParameters.ValidateLifetime = true;
options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Tokens:Key"]));
options.TokenValidationParameters.ValidIssuer = Configuration["Tokens:Issuer"];
options.TokenValidationParameters.ValidAudience = Configuration["Tokens:Audience"];
});
services.AddTransient<Services.IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.Configure<SMSoptions>(Configuration);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
IMapper _mapper = mappingConfig.CreateMapper();
services.AddSingleton(_mapper);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors("AllowEverything");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
//app.UseHsts();
}
app.UseCors("AllowEverything");
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
Solution
I tried a lot of different solution, in the end I found there is a unknown error on http interceptor and what I think is that when System is maxed at the CPU usage then this happens, as angular isn't able to complete the call and it goes into unknown error as CPU has no room to work on it. Please let me know if that makes sense or something that can happen with heavy applications.
Thank you
Answered By - Hassan Arif
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.