WebClient ignoring credentials

  .net-core, c++, linux, smb, windows

Goal: Connecting to an SMB share using .net core, program has to be platform independent (no winapi calls etc).

My research on SO: Tons of topics on SO cover this, but none of them have worked for me, either because they use winapi or because the code samples they show simply dont work.

My attempts: Up until recently I’ve been using winapi myself, however that’s no longer an option. I’ve tried the many samples using CredentialCache and none of them have worked. Here’s my current attempt and cause of suffering:

var cred = new NetworkCredential { Username = "user", Password = "pass" };
using var wc = new WebClient();
wc.Credentials = cred;
var bytes = wc.DownloadData(new Uri(@"serversharefile"));

If I run this code while storing the credentials to the share in the windows credential manager, it will work (duh), as soon as I remove these credentials it will start throwing user name or password incorrect exceptions.

The credentials I’m setting are correct and I’ve confirmed by using wireshark that the credentials I’m setting are NOT the credentials that are being used to connect. Instead, my logon credentials are used.

I’ve been looking at the source code here: WebClient source and I see no reason why the credentials I’m providing would be ignored.

What am I overlooking here?

Source: Windows Questions