HttpClient.Post forbidden from IIS - c#

I have a C# / WCF service which needs to process a post request to a third party company. It sends documents to this outsourcing company. The C# code, use a HttpClient.PostAsync and works without any problems on Visual Studio 2013 with IIS Express. I deployed a self hosted WCF service on my server and it still works ! But, when I deploy this code on IIS (on the same server), I have the following exception : System.Web.HttpException (0x80004005): Forbidden.
Here are exception details :
HTTP Code [Forbidden]
Phrase="Forbidden"
Message="Method: POST, RequestUri: 'https://third.party.company/api/sandbox//users/73437a40-3827-4df6-855f-c58c00750007', Version: 1.1, Content: System.Net.Http.MultipartFormDataContent, Headers:
{
Accept: application/vnd.v1+json
Authorization: Bearer *****
Content-Type: multipart/form-data; boundary="ec9e6a56-103a-4728-a152-d86e836fe62e"
Content-Length: 138986
}" Result = [<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /api/sandbox//users/73437a40-3827-4df6-855f-c58c00750007
on this server.</p>
</body></html>
]
There can be two problems :
The post request is too big
There is an hidden setting in IIS which blocks the request
The server runs under Windows Server 2012 R2 and IIS 8.5
Can you help me ?
Many Thanks !

The problem comes from the two slash in the post URL :
https://third.party.company/api/sandbox//users/73437a40-3827-4df6-855f-c58c00750007
I works whitout problems with only one.
I do not understand, why there is no error on IIS logs and why the url is truncated before api instead of users

Related

Best option to interface a Xamarin.Forms app with Azure SQL Server

I'm trying to study a use case where I have a Xamarin.Forms application which interfaces with the Azure platform; in particular, with an SQL Database.
I followed this guide for the Xamarin part, and created a Web App and a SQL Database (with a SQL Server) for the Azure part, like shown here.
So I created a table in the database and modified the .NET backend of the guide
replacing the TodoItem class with the new class that represents my table.
After the deployment, nothing seemed to be changed.
The Xamarin code, by the way, with the class Microsoft.WindowsAzure.MobileServices.MobileServiceClient, seems to be calling a REST API; the connection to the web app is successful, but there's an error on a request like this:
Invalid sync operation: The request could not be completed. (Internal Server Error)Method: GET, RequestUri: 'https://myapp.azurewebsites.net/tables/Address?$filter=not(complete)', Version: 2.0, Content: <null>, Headers:
{
X-ZUMO-FEATURES: TT
X-ZUMO-INSTALLATION-ID: 3d018c77-7c83-4635-9592-31cdbbb4c21a
Accept: application/json
User-Agent: ZUMO/4.1
User-Agent: (lang=Managed; os=Windows Store; os_version=--; arch=X86; version=4.1.1.0)
X-ZUMO-VERSION: ZUMO/4.1 (lang=Managed; os=Windows Store; os_version=--; arch=X86; version=4.1.1.0)
ZUMO-API-VERSION: 2.0.0
Accept-Encoding: gzip
}
Accessing https://myapp.azurewebsites.net/tables/Address from a Web browser, in fact, returns an error.
Have you ever experienced something like this? I cannot find any REST service in Azure. The MobileServiceClient seems very good, it provides a very good level of abstraction, so I would prefer something like this than a custom REST client.
Thank you.

How does Kestrel know the virtual directory it is running under in IIS?

We are trying to migrate our aspnetcore web apps from our own Windows/IIS server to AWS using ECS and API Gateway. We have everything working but ran into an unexpected issue with redirects inside our web applications. Our web site is setup like this in IIS:
/ => the root website is pointing to another web app
/app1 => app1 is setup as a virtual directory pointing to an empty folder
/app1/v1 => v1 is setup as an application pointing to an aspnetcore app
/app2/v1 => again v1 is setup as an application pointing to a different aspnetcore app
I have a very simple out of the box aspnetcore application that is setup as a virtual directory under /app1/v1. The routes in the aspnetcore application are:
/Web
/Web/Login
/Web/Home
When a user first visits the site (http://localhost/app1/v1/Web) they are redirected to the login page using:
Redirect("~/Web/Login")
When the application is run by IIS that results in the browser being redirected to:
http://localhost/app1/v1/Web/Login
However when I use a reverse proxy, like AWS API Gateway, that has been setup with the same virtual path it redirects to:
http://localhost/Web/Login
I'm guessing that there must be a configuration value that I need to pass into Kestrel during startup or as a request header to tell it to add "/app1/v1/" to any ~/ redirects that it performs?. Does anyone know how IIS is telling Kestrel the full path and how I can replicate that behavior?
Whenever asp.net runtime redirects,it will always use the base path
from the root .In your case the problem is ~ passed.This is telling
the browser to redirect from the base path which is /.
You can do remove the ~ and try giving Redirect("Web/Login").But I have seen asp.net runtime always add Reidrect Response wiht Location Header as "~/Web/Login".
So if removing the ~ does not work,you can try with
Response.RedirectToAbsoluteUrl("Web/Login");
Edit:
To explain the scenario why this may be happening when you do reverse proxy
Normally you setup a reverse proxy like this
www.example.com => servername:8080
When the proxy forwards the request,server(asp.net) does not know anything about the www.example.com ,to that asp.net runtime the request hostname is servername.
Now when you do redirect, what the asp.net runtime sends is a 302 response
This will look like this
Client request:
GET /app1/v1/Webl HTTP/1.1
Host: localhost
Server response:
HTTP/1.1 302 Found
Location: ~/Web/Login
Please check out this with a fiddler if you would like to confirm
Now when behind a reverse proxy where you may have setup like this
www.example.com/myapp => servername:8080
www.example.com/anotherapp => servername2:8080
You can double check how is the reverse proxy setup.Now imagine the same 302 response but your application does not know what is the relative path (www.example.com/myapp) .This causes problem with the what Location header is send in the response(if it has ~,this causes problems with relative paths in the original URL .
Hope this helps!.

C# - Sending Notifications with Firebase returns error 500

I am trying to send a notification from my server (C#) to Firebase. But it seems that Firebase keeps returning:
System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
I have, after going over the simple code, I tried to test it on Postman (Chrome extension):
Request:
POST
https://fcm.googleapis.com/fcm/send
headers:
Authorization: key=1:********:android:90d906ac519686d0 (i got this from the firebase console under App id in project settings)
Content-Type: application/json
Sender: id=******office-906 (i got this from firebase console under ProjectId)
body:
{ "to" : "dlDtgNcjWXQ:APA91bHTJdgfrOG345EAz0muNdgoxoDEJ_LUKCIK61R1esPLAyLemPRrV0sTcve3RG20FNvs8pt....U81uRZ9WZ32EtXL2wH0hXSruOddz"
}
Response:
<HTML>
<HEAD>
<TITLE>Internal Server Error</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Internal Server Error</H1>
<H2>Error 500</H2>
</BODY>
So if I ignore C# for a second, it seems I get the same error from Postman as well. What am I doing wrong? I read the guides and documentations. Any ideas?
According to the docs:
https://firebase.google.com/docs/cloud-messaging/server#implementing-http-connection-server-protocol
https://firebase.google.com/docs/cloud-messaging/send-message
Authorization: key=YOUR_SERVER_KEY
Make sure that YOUR_SERVER_KEY is the server key, whose value is visible in your Firebase Project Console under Project Settings > Cloud Messaging Tab
You seem to be using the app id (1:********:android:90d906ac519686d0). Also, the Sender header is not required or documented.

Downloading files to remote server HTTP 400 error

Program uses DownloadString function of Web.Client.
The url's I have tried so far:
http://xxx.xxx.x.xx/blabla.ashx?command=blabla - Http 404
http://xxx.xxx.x.xx:port/blabla.ashx?command=blabla - Http 400
When I type
http://xxx.xxx.x.xx
IIS page shows up as expected.
Problem solved now. It seems you have to add a binding to IIS to use an address in remote machine.

How can I handle a custom POST origin with Nancy?

I have a third party server that POSTs data to my C# Nancy console application with the following request;
POST //172.16.100.20 HTTP/1.1
User-Agent: P2000/3.6.0
Host: 172.16.100.20:40000
Server: remotesitename
Content-Type: text/xml
Content-Length: 2744
<MessageBase>
<BaseVersion>301</BaseVersion>
<MessageType>3</MessageType>
....
However I just can't seem to get Nancy to receive the request. I've tried various catchall routes such as
"(.*)"
"{uri*}"
"//172.16.100.20"
But none of them work with the above request (they generally work from a browser or Fiddler).
I've also tried hooking in to the module and application Before handlers, but they don't fire too.
The same code works if I use a correct request from my own test applications with ORIGIN as follows;
http://172.16.100.2
This simulated request was issued from the same third party server to the listening Nancy server over the local network and without firewalls or virus scanners.
Any ideas?
Cheers
Dave

Categories

Resources