I am building a new common Authentication module for my application. For this, I need to intercept requests coming to API layer, route to the common Authentication service and continue with regular flow only if Authentication service returned success. When invoking Authentication service, parameters are passed to it, via query string, based on which it returns true/false as response.
Flow of events:
Browser invokes http://localhost/SampleService/api/Home?param=data i.e Site1
Reverse proxy rule on Site1 makes the call go to Site2 which is http://localhost/ValidateAuthN
Site2 checks the value of param and returns a JSON response - true or false
Browser gets the response back.
I am using IIS as reverse proxy with url rewrite module and ARR configured. "Enable proxy setting" has been checked for ARR on root node in IIS. On API site in IIS, I have configured rewrite rule as below:
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://localhost/ValidateAuthN" />
</rule>
</rules>
</rewrite>
I see that the request is redirected to Authentication service with query strings values and the true/false response returned by Authentication service is received directly in calling application. However, request is not reaching the actual API layer for further processing. What I want is that at step 4 above, the request should go back to site1 with JSON response of true/false.
Please suggest what could I be missing in this configuration.
Maybe you need an exception rule for the reply? Your match rule (.*) catches everything, so the true/false reply might be getting re-routed itself?
Related
We have an Angular web application running on Azure. The backend is Dot Net Core 3.1. The Authentication is done through Azure AD. What is happening is the following steps
User logs into the web site and navigates to a page. Everything is working as expected
The user logs out, but does not close the tab. The Logout screen is shown as expected
The user opens a new tab and navigates to the web site
The user is NOT prompted for a login and when the user navigates to page they get error
Access to XMLHttpRequest at 'https://login.microsoftonline.com/2aa666ca-a53c-4274-bfcc-41b83867d22a/oauth2/authorize?client_id=794424bb-0ad2-41f8-b007-b71ed576b793&redirect_uri=https%3A%2F%2Fmydomain.azurewebsites.net%2Fsignin-oidc&response_type=id_token&scope=openid%20profile&response_mode=form_post&nonce=637782164067215548.NzQ4MWQxNjgtYjdlZS00OTlmLWIwYzctM2NhZWRkZjdhMTZiYzdmNjljOTEtNTk3OS00NDlhLWEzZjgtODZkN2YyZDZmYzlh&state=CfDJ8IE9b1M0cDdJqtQPg1_KxRR0vlYAN1zOfKzhpzhdzqvcX_XGygV8nincOzoVYDPPyZWnbh5SrGMDZhQfmUDlO4wQ41v_7Q_gJCUHftetejZZQZTS7Uhn-IVBRysh36hvldRY2pxcZBKCQHLYkKMnR2my9R0TqsaqEAI4gGNUUHwa8fJnv4xj0lkMJq_DORhJS4AwLMhtQWGvuIp0gzQ_cGR0gjGZvMRMTaBZunGBDexThOzzbAyQJTCJuNxUUB_tvAm0cFEVWb3lTPYRgk1ARnagmg7a6GDFrCnXT7vT_3VJjUyMzrazbt1xeRtrs1AdlUIX1fnnFsGZPGUBA3kYvzTyEgVkm97FGBTPgeGZAP3W&x-client-SKU=ID_NETSTANDARD2_0&x-client-ver=5.5.0.0' (redirected from 'https://mydomain.azurewebsites.net/api/profileTypes?_=1642619606751') from origin 'https://mydomain.azurewebsites.net' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
If the user refreshes the page, then they are forced to login and everything begins working again
I have tried adding in CORS as directed on other questions and other websites as follows
In the ConfigureService function of the startup.cs The following code is present
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
{
builder
.WithOrigins(new[] { "http://YOUR_FRONTEND_ORIGIN" })
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
}));
In the Configure function of the startup.cs is the following code
app.UseCors("CorsPolicy");
I am confused as to why CORs would even come into play as everything is on the same Azure subscription, but I added the above code and it does not change anything. Searching the internet give mainly the same or similar implementations as above.
I am not sure how to proceed or even what questions to ask, so any help would be appreciated
Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism which basically allows server to indicate any origins other than its original url or scheme or domain or port .But cors settings may also vary from different browsers which may cause this error.
In case of Azure App Service it sets “AppServiceAuthSession” cookie for authenticated session with browser. The web application may use XMLHttpRequest / AJAX request inside the app and the request sent may contain the AppServiceAuthSession cookie. When this cookie is still present in the browser and the web page pointing to the page of app may not be the original url and which might be the possible cause of error.When this cookie is not present in the request, Azure App Service will redirect the request to Azure AD for login that is why when you referesh it, it works.
If the browser is enabled with cors ,it makes a pre-flight cors check to the specified url and gets the access-control-allow-origin headers, we can modify this headers accordingly,if origin restriction is not required.
Try to insert the http protocol present in the asp.net Web Api backend, in the Web.config between the <system.webServer> </ system.webServer> tags,
Example:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
</system.webServer>
and you can try enable access to all origins from browser settings .
Please check this reference access to xmlhttprequest has been blocked by cors policy Code Example (codegrepper.com) if you can enable credentails to true and by using cors variable to use in app.use(cors).
References:
Cross-origin XMLHttpRequest - Chrome Developers
express - Angular CORS request blocked - Stack Overflow
I have a apiapp in azure that the webapp is calling. I have the apiapp setting cookies on the webapp for state, this all works fine when its the same domain but im moving to Azure App Services and the cookies are coming back in the headers with the domain set but nothing is being displayed on the cookies tab in chromium. Both sites are Https ... is it perhaps that CORS is setup wrong. I am using Node for the api if that means anything and right now i have the domains in the below screen shots hard coded.
The api app has the CORS origin set to https://ep-webapp.azurewebsites.net in app services.
The locale cookie is being set by the webapp
So... is this even possible .. is there a better way to do this
I was able to route the calls in Azure using a reverse proxy...
https://ruslany.net/2014/05/using-azure-web-site-as-a-reverse-proxy/
WEBSITE_PRIVATE_EXTENSIONS set to 1 in the App Settings for the site.
<rule name="Proxy" stopProcessing="true">
<match url="^api/?(.*)"/>
<action type="Rewrite" url="https://apiapp.azurewebsites.net/{R:1}"/>
</rule>
I created a ARM template that creates App Service (web application) and Application Insights resource that is similar to this template (https://github.com/tomasr/webapp-appinsights). The App insights is connected to web app and everything works fine, the only problem is that app insights automatically generates requests to root of my web app as part of availability test that always return error because I don't have anything in the root path (even if I had, it is calling http instead of https url). There is nothing in the template that sets up this request, and if I go to availability properties, nothing is there. Is there a way to turn this feature off or configure it to call a valid URL?
In case it's Always On, the feature of App Service that keeps your app from going idle by pinging it with an HTTP GET to the root every once in a while, there is a way with Rewrite.
You can make a rewrite rule that has a condition that detects it's an Always On request, and if it is, make it hit another URL you specify instead.
Here is an example:
<?xml version="1.0" encoding="UTF-8" standalone="no">
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite AlwaysOn" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{HTTP_USER_AGENT}" pattern="^AlwaysOn$" />
</conditions>
<action type="Rewrite" url="/api/Online/Ping" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Original article I found this from: https://blogs.msdn.microsoft.com/benjaminperkins/2018/08/10/how-to-optimize-azure-app-service-alwayson-cold-start-warm-up-request/
If you have a rewrite rule that redirects HTTP to HTTPS, you should add this rule before that.
My marketing team accidentally created materials with an additional 'www' sub domain and I need to see if I can remove it using Azure traffic manager.
Marketing domain (incorrect) HTTPS://www.my.site.com
Desired domain HTTPS://my.site.com
I only have a wildcard cert for *.site.com so at this time I cannot support the 'www'.
If the user enters the 'www' site over HTTP I can successfully redirect to the correct site. If they enter the HTTPS, or use Firefox that defaults to HTTPS, they will see a certificate error for HTTPS://www.my.site.com.
Would a second wildcart cert for *.my.site.com help? I am not sure where I would configure it.
Any help would be appreciated, thanks.
The certificate you have should be enough since its a wildcard cert, no need to buy another one.
I think this can be fixed by adding another host-name to your app and creating another SSL binding.
That means you need to add the www.my.site.com host-name (make sure to assign it to the traffic manager url):
And then do the binding:
Once that is done both URLs will work through SSL.
If you want to then have all traffic from going to you can then add a URL re-write rule:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="SPA">
<match url="OLDURL" />
<action type="Rewrite" url="NEWURL" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
You can look at this other answer to get more data: Rewriting a URL in an Azure web app
I have registered a domain name for most, if not all, countries for my domain. My website is running under Azure Websites.
What I am looking at doing is redirecting all non-.com domains to the .com domain, but including an additional parameter specifying the country and/or language (I am undecided on this, but it is the mechanism for getting this working that is the main point).
For example:
User visits example.com.au, they are redirected to example.com/au
User visits example.co.uk, they are redirected to example.com/uk
User visits example.com, we append the country to give example.com/us
I believe that this might be able to be performed via URL rewrite rules plus the correct DNS entries, but not sure where to start. In addition, does this allow me to get away with a single SSL certificate for the .com domain only?
I ended up implementing this via URL Rewrite rules.
<rules>
<rule name="autocom" enabled="true" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com\.au"/>
</conditions>
<action type="Redirect" url="http://example.com/au/{R:1}" redirectType="Permament"/>
</rule>
</rules>
The rule simply looks for the incoming host name, and redirects to my desired URL, appending the query string.
The DNS entries simply have an A record pointing to website, and the Azure Website knows about the non-.com domain name as well.
So far, this hasn't caused me any issues with regard to the SSL certificate - all browsers I tested have not shown their invalid SSL certificate warning(s).