2

I'm working on a Petrel plugin that makes a web request to an IIS server that requires Windows Authentication. In order to send the Windows credentials of the currently logged in user, I set request.UseDefaultCredentials = true. Below is the code to make the request.

public static void MakeWebRequest()
{
    var request = (HttpWebRequest)WebRequest.Create("some web address");
    request.ContentType = "application/json; charset=utf-8";
    request.Method = "POST";
    request.UseDefaultCredentials = true;
    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
    {
        streamWriter.Write("some request data");
    }

    var response = request.GetResponse();

    using (var streamReader = new StreamReader(response.GetResponseStream()))
    {
        var responseText = streamReader.ReadToEnd();
    }
}

When running this code from a simple console application, the logged in user's Windows credentials are successfully added to the web request and a response is returned.

However, when running the same code inside of a Petrel plugin, the credentials are not added. This results in IIS rejecting the request with a 401 Unauthorized error.

Why does this code not work inside of a plugin? Is there any way to attach the current user's Windows credentials to the web request? I don't want to have to prompt the user inside the plugin for credentials.

  • Have you tried setting the credentials through `request.Credentials = CredentialCache.DefaultCredentials;` – GETah Nov 24 '16 at 08:15
  • Yes, I've tried `request.Credentials = CredentialCache.DefaultCredentials;` and `request.Credentials = CredentialCache.DefaultNetworkCredentials;`. – Snuggles-With-Bunnies Nov 29 '16 at 21:45
  • Experimenting some more, I've found that the web request works when called inside the main module class (the class that derives from `IModule`. The module sets up a `LoaderFactory`. Making the web request in the `LoaderFactory` or at any level deeper causes the request to fail. I'm not sure how `DefaultCredentials` could just disappear. – Snuggles-With-Bunnies Nov 30 '16 at 22:34
  • Can you inspect the credentials object to see if there is anything in there? Or is it set to null? – GETah Nov 30 '16 at 23:54

1 Answers1

-1

I don't know why, but apparently Petrel filters out your credentials. However, I think you can get the credentials. Google for HttpWebRequest CreateCredentials.