Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authenticate with Azure AD using ASPNET Core 2 from behind Corporate Proxy

I have an ASPNET Core 2 application which I am trying to Authenticate with Azure AD using OpenId. I just have boilerplate code from selecting Single Organization Authentication in the ASPNET Core 2 templates, so no custom code. I followed the article here.

The app is not able to get metadata from the Azure AD application because of proxy. The same URL returns data if I just paste it in browser.

The error I get is:

HttpRequestException: Response status code does not indicate success: 407 (Proxy Authentication Required).

System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() IOException: IDX10804: Unable to retrieve document from: 'https://login.microsoftonline.com/my-tenant-id/.well-known/openid-configuration'.

Microsoft.IdentityModel.Protocols.HttpDocumentRetriever+d__8.MoveNext()

I have another ASPNET 4.5.2 application where I am able to perform authentication with the same Azure AD app as above after setting proxy in code like below:

System.Net.HttpWebRequest.DefaultWebProxy = new WebProxy
        {
            Address = new Uri("http://my-company-proxy:8080"),
            Credentials = new NetworkCredential
            {
                UserName = "proxyusername",
                Password = "proxypassword"
            }
        };

So Essentially my problem is to get past the Proxy Authentication in ASPNET Core 2.

I have tried Microsoft.AspNetCore.Proxy package. Its pretty much broken and doesn't work for me. Also I tried adding the Proxy entries in machine.config (which are actually not required for 4.5.2 app) but that doesn't work as well. I believe getting past a corporate proxy should be very trivial, but doesn't look like it so far.

like image 648
Abhishek Tiwari Avatar asked Apr 02 '18 22:04

Abhishek Tiwari


1 Answers

Tratcher's comment pointed me in the right direction and I got it working, but just to help everyone with it, below is what you need to do:

  builder.AddOpenIdConnect(options => options.BackchannelHttpHandler = new HttpClientHandler
        {
            UseProxy = true,
            Proxy = new WebProxy
            {
                Credentials = new NetworkCredential
                {
                    UserName = "myusername",
                    Password = "mypassword"
                },
                Address = new Uri("http://url:port")
            }
        });
like image 93
Abhishek Tiwari Avatar answered Oct 02 '22 11:10

Abhishek Tiwari