NetworkCredential default constructor leaves Domain null, leading to null object reference exceptions in framework code. - by MattMitchell

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 695227 Comments
Status Closed Workarounds
Type Bug Repros 1
Opened 10/16/2011 10:52:37 PM
Access Restriction Public



The System.Net.NetworkCredential class has a Domain property.

This property is enforced to never be null through a guard in the property setter, and through constructor chaining on the constructors that do not take a domain.

However, the default constructor does not chain, and as a result Domain (and thus its private member variable, "m_domain" in my decompiled source) are left null - a condition that could not be obtained explicitly.

This issue extends to the other string properties of the NetworkCredential class (e.g. UserName / Password).

This wouldn't be such a big issue except:
1) The rest of the code in NetworkCredential protects Domain from being null.
2) Some code assumes Domain cannot be null (e.g. InternalGetDomainUsername() method used by FtpControlStream and HttpWebRequest).

A workaround at this stage is to explicitly set Domain to anything (including null, as this is guarded to set the Domain to String.Empty), or to not use the default NetworkCredential constructor.

However, it's a quite subtle bug given that the following produce different behaviour:

new NetworkCredential(
  "user", "pass"
 ); // Domain == String.Empty

new NetworkCredential(
  userName: "user", password: "pass"
 ); // Domain == String.Empty

new NetworkCredential()
  Username = "user",
  Password = "pass"
}; // Domain == null

var creds = new NetworkCredential();
creds.Username = "user";
creds.Password = "pass";
// Domain == null

Using the last methods (where Domain is null) leads to null reference exceptions emerging during FtpWebRequest usage (e.g.

Using a NetworkCredential instance created using the default constructor as follows produces a WebException with status UnknownError in FtpWebRequest (a wrapped NullReferenceException):

NetworkCredential credentials = new NetworkCredential();
credentials.UserName = "user";
credentials.Password = "pass";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://localhost:21/");
request.Credentials = credentials;
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

// Throws error
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
Sign in to post a comment.
Posted by Microsoft on 10/20/2011 at 10:50 AM
Thank you for the detailed feedback. We will plan to address this in a future release.
Posted by EricLeong [Feedback Moderator] on 10/17/2011 at 4:35 AM
Thank you for submitting feedback on Visual Studio 2010 and .NET Framework. Your issue has been routed to the appropriate VS development team for review. We will contact you if we require any additional information.
Posted by MS-Moderator01 on 10/16/2011 at 11:41 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(