I have the following code:
var baseUrl = "https://" + GetIdentityProviderHost(environment) + "/oauth2/authorize";
var query = $"?scope=openid&response_type=code&redirect_uri={redirectUrl}&client_id={clientId}";
var combinedUrl = baseUrl + query;
var currentUser = WindowsIdentity.GetCurrent();
await WindowsIdentity.RunImpersonated(currentUser.AccessToken, async() =>
{
using (var client = new WebClient{ UseDefaultCredentials = true })
{
var response = client.DownloadString(combinedUrl);
Console.WriteLine(response);
}
});
It basically constructs a URL and then calls it.
The call returns with a 401 (Unauthorized).
But if I take the combinedUrl
and paste it into chrome or postman it works perfectly. That tells me that my call can work because Chrome is using my Windows Credentials to make the call.
I added the WindowsIdentity.RunImpersonated
code to try to get around this issue. But it seems to not have had any effect.
How can I make a web call using Integrated Windows Authentication (IWA)?
Details:
If I run the following cURL command it works:
curl -L --negotiate -u : -b ~/cookiejar.txt "https://myIdp.domain.net/oauth2/authorize?scope=openid&response_type=code&redirect_uri=https://localhost:5001&client_id=my_client_id_here"
I am not sure how to replicate all that in C# code.
FYI: I have asked about this cURL command specifically in this question (since this question was focused on impersonation): Replicate cURL Command Using Redirect and Cookies in .Net Core 3.1