Snuggles-With-Bunnies
Snuggles-With-Bunnies

Reputation: 111

Using DefaultCredentials inside a Petrel plugin

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.

Upvotes: 2

Views: 131

Answers (1)

user7047425
user7047425

Reputation: 19

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

Upvotes: -1

Related Questions