Edit

Share via


CredentialCache.Add Method

Definition

Adds a NetworkCredential instance to the credential cache.

Overloads

Add(Uri, String, NetworkCredential)

Adds a NetworkCredential instance to the credential cache for use with protocols other than SMTP and associates it with a Uniform Resource Identifier (URI) prefix and authentication protocol.

Add(String, Int32, String, NetworkCredential)

Adds a NetworkCredential instance for use with SMTP to the credential cache and associates it with a host computer, port, and authentication protocol. Credentials added using this method are valid for SMTP only. This method does not work for HTTP or FTP requests.

Add(Uri, String, NetworkCredential)

Source:
CredentialCache.cs
Source:
CredentialCache.cs
Source:
CredentialCache.cs

Adds a NetworkCredential instance to the credential cache for use with protocols other than SMTP and associates it with a Uniform Resource Identifier (URI) prefix and authentication protocol.

public:
 void Add(Uri ^ uriPrefix, System::String ^ authType, System::Net::NetworkCredential ^ cred);
public void Add (Uri uriPrefix, string authType, System.Net.NetworkCredential cred);
member this.Add : Uri * string * System.Net.NetworkCredential -> unit
Public Sub Add (uriPrefix As Uri, authType As String, cred As NetworkCredential)

Parameters

uriPrefix
Uri

A Uri that specifies the URI prefix of the resources that the credential grants access to.

authType
String

The authentication scheme used by the resource named in uriPrefix.

cred
NetworkCredential

The NetworkCredential to add to the credential cache.

Exceptions

uriPrefix is null.

-or-

authType is null.

The same credentials are added more than once.

Examples

The following code example initializes a CredentialCache with multiple security credentials and uses those credentials with a WebRequest.

CredentialCache^ myCache = gcnew CredentialCache;

myCache->Add( gcnew Uri( "http://www.contoso.com/" ), "Basic", gcnew NetworkCredential( UserName,SecurelyStoredPassword ) );
myCache->Add( gcnew Uri( "http://www.contoso.com/" ), "Digest", gcnew NetworkCredential( UserName,SecurelyStoredPassword,Domain ) );

wReq->Credentials = myCache;
CredentialCache myCache = new CredentialCache();

myCache.Add(new Uri("http://www.contoso.com/"),"Basic",new NetworkCredential(UserName,SecurelyStoredPassword));
myCache.Add(new Uri("http://www.contoso.com/"),"Digest", new NetworkCredential(UserName,SecurelyStoredPassword,Domain));

wReq.Credentials = myCache;
Dim myCache As New CredentialCache()

myCache.Add(New Uri("http://www.contoso.com/"), "Basic", New NetworkCredential(UserName, SecurelyStoredPassword))
myCache.Add(New Uri("http://www.contoso.com/"), "Digest", New NetworkCredential(UserName, SecurelyStoredPassword, Domain))

wReq.Credentials = myCache

Remarks

The Add method places a NetworkCredential instance for use with protocols other than SMTP into the CredentialCache. The cache stores credentials in the order in which they are added to it. When the GetCredential(Uri, String) method is called, it returns the proper matching NetworkCredential instance.

Applies to

Add(String, Int32, String, NetworkCredential)

Source:
CredentialCache.cs
Source:
CredentialCache.cs
Source:
CredentialCache.cs

Adds a NetworkCredential instance for use with SMTP to the credential cache and associates it with a host computer, port, and authentication protocol. Credentials added using this method are valid for SMTP only. This method does not work for HTTP or FTP requests.

public:
 void Add(System::String ^ host, int port, System::String ^ authenticationType, System::Net::NetworkCredential ^ credential);
public void Add (string host, int port, string authenticationType, System.Net.NetworkCredential credential);
member this.Add : string * int * string * System.Net.NetworkCredential -> unit
Public Sub Add (host As String, port As Integer, authenticationType As String, credential As NetworkCredential)

Parameters

host
String

A String that identifies the host computer.

port
Int32

A Int32 that specifies the port to connect to on host.

authenticationType
String

A String that identifies the authentication scheme used when connecting to host using cred.

credential
NetworkCredential

The NetworkCredential to add to the credential cache.

Exceptions

host is null.

-or-

authType is null.

authType not an accepted value.

port is less than zero.

Examples

The following code example initializes a CredentialCache with multiple security credentials for use with SMTP and uses one of those credentials with a SmtpClient.

SmtpClient^ client = gcnew SmtpClient("ContosoMail", 45);
MailAddress^ from = gcnew MailAddress("[email protected]", "Sender Name");
MailAddress^ to = gcnew MailAddress("[email protected]", "Recepient Name");
MailMessage^ message = gcnew MailMessage(from, to);

message->Body = "This is a test email message sent by an application. ";
message->Subject = "Test Email using Credentials";

NetworkCredential^ myCreds = gcnew NetworkCredential("username", "password", "domain");
CredentialCache^ myCredentialCache = gcnew CredentialCache();        
try 
{
    myCredentialCache->Add("ContoscoMail", 35, "Basic", myCreds);
    myCredentialCache->Add("ContoscoMail", 45, "NTLM", myCreds);
            
    client->Credentials = myCredentialCache->GetCredential("ContosoMail", 45, "NTLM");
    client->Send(message);
    Console::WriteLine("Goodbye.");
}
    catch(Exception^ e)
    {
          Console::WriteLine("Exception is raised. ");
          Console::WriteLine("Message: {0} ",e->Message);
    }
SmtpClient client = new SmtpClient("ContosoMail", 45);
MailAddress from = new MailAddress("[email protected]", "Sender Name");
MailAddress to = new MailAddress("[email protected]", "Recepient Name");
MailMessage message = new MailMessage(from, to);

message.Body = "This is a test email message sent by an application. ";
message.Subject = "Test Email using Credentials";

NetworkCredential myCreds = new NetworkCredential("username", "password", "domain");
CredentialCache myCredentialCache = new CredentialCache();
try
{
    myCredentialCache.Add("ContoscoMail", 35, "Basic", myCreds);
    myCredentialCache.Add("ContoscoMail", 45, "NTLM", myCreds);

    client.Credentials = myCredentialCache.GetCredential("ContosoMail", 45, "NTLM");
    client.Send(message);
    Console.WriteLine("Goodbye.");
}
    catch(Exception e)
    {
          Console.WriteLine("Exception is raised. ");
          Console.WriteLine("Message: {0} ",e.Message);
    }
Dim client As New SmtpClient("ContosoMail", 45)
Dim from As New MailAddress("[email protected]", "Sender Name")
Dim sendTo As New MailAddress("[email protected]", "Recepient Name")
Dim message As New MailMessage(from, sendTo)

message.Body = "This is a test email message sent by an application. "
message.Subject = "Test Email using Credentials"

Dim myCreds As New NetworkCredential("username", "password", "domain")
DIm myCredentialCache As New CredentialCache()

Try 
    myCredentialCache.Add("ContoscoMail", 35, "Basic", myCreds)
    myCredentialCache.Add("ContoscoMail", 45, "NTLM", myCreds)

    client.Credentials = myCredentialCache.GetCredential("ContosoMail", 45, "NTLM")
    client.Send(message)
    Console.WriteLine("Goodbye.")
Catch e As Exception
    Console.WriteLine("Exception is raised. ")
    Console.WriteLine($"Message: {e.Message} ")
End Try

Remarks

This method places a NetworkCredential instance for use with SMTP into the CredentialCache. The cache stores credentials in the order in which they are added to it. When the GetCredential(String, Int32, String) method is called, it returns a NetworkCredential instance that is selected by matching the host, port, and authType. The comparison is done case-insensitively.

The supported values for authType are "NTLM", "Digest", "Kerberos", and "Negotiate".

Credentials added with this method are only valid for use with SMTP. This method does not work for HTTP or FTP protocols.

Applies to