<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>No &#8216;Access-Control-Allow-Origin&#8217; header &#8211; ASP.NET Hosting Reviews and Guides</title>
	<atom:link href="https://topreviewhostingasp.net/tag/no-access-control-allow-origin-header/feed/" rel="self" type="application/rss+xml" />
	<link>https://topreviewhostingasp.net</link>
	<description>ASP.NET Hosting &#124; Reviews &#124; Tips &#38; Tutorial</description>
	<lastBuildDate>Wed, 14 Jun 2023 08:18:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://topreviewhostingasp.net/wp-content/uploads/2017/01/cropped-trhaico-32x32.png</url>
	<title>No &#8216;Access-Control-Allow-Origin&#8217; header &#8211; ASP.NET Hosting Reviews and Guides</title>
	<link>https://topreviewhostingasp.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Fix No &#8216;Access-Control-Allow-Origin&#8217; .NET API CORS</title>
		<link>https://topreviewhostingasp.net/how-to-fix-no-access-control-allow-origin-net-api-cors/</link>
					<comments>https://topreviewhostingasp.net/how-to-fix-no-access-control-allow-origin-net-api-cors/#respond</comments>
		
		<dc:creator><![CDATA[Jacques Hunt]]></dc:creator>
		<pubDate>Wed, 14 Jun 2023 08:15:55 +0000</pubDate>
				<category><![CDATA[Hosting Tips]]></category>
		<category><![CDATA[asp net]]></category>
		<category><![CDATA[asp net tips]]></category>
		<category><![CDATA[asp net tutorial]]></category>
		<category><![CDATA[how to fix No 'Access-Control-Allow-Origin' header]]></category>
		<category><![CDATA[No 'Access-Control-Allow-Origin' header]]></category>
		<guid isPermaLink="false">https://topreviewhostingasp.net/?p=3571</guid>

					<description><![CDATA[In this article, I will discuss about the error message that you might found when calling a CORS protected .NET Core Api. The following is the full error message: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>In this article, I will discuss about the error message that you might found when calling a CORS protected .NET Core Api. The following is the full error message:</p>
<pre class="lang:default decode:true">Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header
is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. 
The response had HTTP status code 405.</pre>
<h2>How to Fix Error No &#8216;Access-Control-Allow-Origin&#8217;</h2>
<h3>1. Allow Method &#8220;Options&#8221;</h3>
<p>A preflight request is one that the browser sends to see if the CORS settings have been correctly implemented.</p>
<p>Therefore, if your endpoint supports POST, PUT, or DELETE operations and the browser sends a &#8220;application/json&#8221; request, the browser sends two requests: the &#8220;OPTIONS&#8221; request first, then a POST, PUT, or DELETE request.</p>
<p>You must therefore enable OPTIONS: in your application&#8217;s CORS settings.</p>
<pre class="lang:default decode:true ">app.UseCors(builder =&gt;
{
  builder
     .WithOrigins("http://localhost:4200", "https://localhost:4200")
     .SetIsOriginAllowedToAllowWildcardSubdomains()
     .AllowAnyHeader()
     .AllowCredentials()
     .WithMethods("GET", "PUT", "POST", "DELETE", "OPTIONS")
     .SetPreflightMaxAge(TimeSpan.FromSeconds(3600));
}
);</pre>
<h3>2. Cors is Defined Twice in Your Code</h3>
<p>Check that you have defined CORS twice.</p>
<p>First add CORS to the <strong>WebApplicationBuilder </strong>Services:</p>
<pre class="lang:default decode:true ">var builder = WebApplication.CreateBuilder();
...
...
builder.Services.AddCors();</pre>
<p>Then when defining the app:</p>
<div>
<pre class="lang:default decode:true ">var app = builder.Build();
app.UseCors(builder =&gt;
      {
        builder
              .WithOrigins("http://localhost:4200", "https://localhost:4200")
              .SetIsOriginAllowedToAllowWildcardSubdomains()
              .AllowAnyHeader()
              .AllowCredentials()
              .WithMethods("GET", "PUT", "POST", "DELETE", "OPTIONS")
              .SetPreflightMaxAge(TimeSpan.FromSeconds(3600));
 
      }
);</pre>
<h3>3. Check the Sequence of Calls</h3>
<p>You must define CORS before you map controllers, define routes etc.</p>
</div>
<div>
<pre class="lang:default decode:true ">var app = builder.Build();
 
// The first thing in the chain of calls is to define the CORS
app.UseCors(builder =&gt;
      {
        builder
              .WithOrigins("http://localhost:4200", "https://localhost:4200")
              .SetIsOriginAllowedToAllowWildcardSubdomains()
              .AllowAnyHeader()
              .AllowCredentials()
              .WithMethods("GET", "PUT", "POST", "DELETE", "OPTIONS")
              .SetPreflightMaxAge(TimeSpan.FromSeconds(3600));
 
      }
);
 
...
...
 
// After that, I can do mapping, routing, redirection etc...
app.MapControllers();
app.UseRouting();
app.UseHttpsRedirection();
 
if (app.Environment.IsProduction())
{
  app.UseTokenAuthentication();
}
 
app.Run();</pre>
<h3>4. Your Load Balancer Must Allow &#8220;Options&#8221; as well</h3>
<p>In front of your API, do you have a load balancer? Verify that OPTIONS methods are also supported by the load balancer.</p>
<p>Hopefully, tips above help you to fix above error. Happy Coding!</p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://topreviewhostingasp.net/how-to-fix-no-access-control-allow-origin-net-api-cors/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
