MSDN Documentation

NetworkCredential Class

Namespace: System.Net.Security

Assembly: System.dll

Represents a network credential consisting of a user's username and password.

Syntax

public sealed class NetworkCredential

Inheritance

Remarks

The NetworkCredential class is used to provide credentials for network authentication. This can include usernames, passwords, and domain names. It is commonly used with classes like NetworkCredential and FtpWebRequest to authenticate with network resources.

When working with sensitive information like passwords, it is important to handle them securely. Consider using more secure storage mechanisms or encryption where appropriate.

Constructors

Properties

Methods

Constructor Details

NetworkCredential()

public NetworkCredential()

Initializes a new instance of the NetworkCredential class with default values (empty strings for username, password, and domain).

NetworkCredential(string userName)

public NetworkCredential(string userName)

Initializes a new instance of the NetworkCredential class with the specified user name. The password and domain will be empty strings.

Parameters

NetworkCredential(string userName, string password)

public NetworkCredential(string userName, string password)

Initializes a new instance of the NetworkCredential class with the specified user name and password. The domain will be an empty string.

Parameters

NetworkCredential(string userName, string password, string domain)

public NetworkCredential(string userName, string password, string domain)

Initializes a new instance of the NetworkCredential class with the specified user name, password, and domain.

Parameters

Property Details

Domain

public string Domain { get; set; }

Gets or sets the domain associated with the network credential. This property can be an empty string if no domain is specified.

Password

public string Password { get; set; }

Gets or sets the password associated with the network credential. This property can be an empty string if no password is provided.

UserName

public string UserName { get; set; }

Gets or sets the user name associated with the network credential. This property can be an empty string if no user name is provided.

Method Details

GetHashCode()

public override int GetHashCode()

Returns the hash code for this instance. The hash code is based on the user name, password, and domain.

ToString()

public override string ToString()

Returns a string representation of the NetworkCredential object. The format is typically "UserName", "Domain\UserName", or "UserName:Password" depending on whether a domain is specified.