Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BasicAuthenticationRequired event removes Content-Type header from Request. #4853

Open
rpriest1260 opened this issue Oct 8, 2024 Discussed in #4841 · 0 comments
Open

Comments

@rpriest1260
Copy link

Discussed in #4841

Originally posted by rpriest1260 September 30, 2024
Hello,

I am seeing an issue and wondering if this is a bug in the WebView2 code. Essentially, after the BasicAuthenticationRequested event fires and the credentials are supplied, the WebView2 code appears to remove the Content-Type header (if supplied), from the initial POST request sent. The webservice I intend to use needs that header, so this breaks the request. Should I file a bug for it? Has anyone else run into this?

Just to check I created the same POST request with a HttpWebRequest, object, and that does not have this problem - it does not strip the Content-Type header after the 407-Proxy Auth Required has been satisfied with the supplied credentials.

I am using:

  • .NET 4.7.2
  • WebView2 1.0.2792.45
  • Proxy Server - with Proxy Authentication Required (I am testing with Fiddler Classic).
  • A web service that accepts a POST request

My POST request is done as follows:

 var postDataStream =  new MemoryStream (Encoding.UTF8.GetBytes (PostData ?? ""));;
 var request = webView.CoreWebView2.Environment.CreateWebResourceRequest(TargetUrl,
   "POST", postDataStream, "Content-Type: application/x-www-form-urlencoded");
 webView.CoreWebView2.NavigateWithWebResourceRequest(request);

And my credentials are supplied using the BasicAuthenticationRequired even, like so

webView.CoreWebView2.BasicAuthenticationRequested += (s, args) =>
    {
        //var creds = new {UserName="1", Password="1"};
        var creds = (webProxy == null || string.IsNullOrEmpty(webProxy?.Address.ToString())) ? null : webProxy?.Credentials as NetworkCredential;
        if (creds != null)
        {
            s_logger.Debug($"Setting proxy creds. User = {creds.UserName}");
            args.Response.UserName = creds.UserName;
            args.Response.Password = creds.Password;
        }
    };

And as mentioned, above, A basic HttpWebRequest, does not have this issue. If you would like to see what code I used to compare to WebView2, it looks like this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);            
WebProxy myProxy = new WebProxy();
// Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
myProxy.Address = new Uri("http://localhost:9191");
// Create a NetworkCredential object and associate it with the 
// Proxy property of request object.
myProxy.Credentials = new NetworkCredential("1", "1");            
request.Proxy = myProxy;

request.Method = "POST";
ASCIIEncoding encoding = new ASCIIEncoding();
request.ContentType = "application/x-www-form-urlencoded";
byte[] data = encoding.GetBytes("data1=postdatastring_fff");
request.ContentLength = data.Length;
Stream newStream = request.GetRequestStream(); //open connection
newStream.Write(data, 0, data.Length); // Send the data.
newStream.Close();
string text;
var response = (HttpWebResponse)request.GetResponse();

if any other information or sample code is needed, please just ask and I will supply.

Thank you in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant