共用方式為


CredentialCache.Add 方法

定義

NetworkCredential 執行個體加入至認證快取。

多載

Add(Uri, String, NetworkCredential)

將要搭配 SMTP 以外其他通訊協定使用的 NetworkCredential 執行個體加入至認證快取,並使其與統一資源識別元 (URI) 和驗證通訊協定產生關聯。

Add(String, Int32, String, NetworkCredential)

將要搭配 Simple Mail Transfer Protocol (SMTP) 使用的 NetworkCredential 執行個體加入至認證快取,並使其與主機電腦、連接埠和驗證通訊協定產生關聯。 使用此方法加入的認證只適用於 SMTP。 這個方法不適用於 HTTP 或 FTP 要求。

Add(Uri, String, NetworkCredential)

來源:
CredentialCache.cs
來源:
CredentialCache.cs
來源:
CredentialCache.cs

將要搭配 SMTP 以外其他通訊協定使用的 NetworkCredential 執行個體加入至認證快取,並使其與統一資源識別元 (URI) 和驗證通訊協定產生關聯。

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)

參數

uriPrefix
Uri

Uri,指定認證允許存取之資源的 URI 前置詞。

authType
String

驗證配置,由在 uriPrefix 中指名的資源所使用。

cred
NetworkCredential

NetworkCredential,要加入至認證快取。

例外狀況

uriPrefixnull

-或-

authTypenull

相同的認證會加入多次

範例

下列程式代碼範例會 CredentialCache 使用多個安全性認證初始化 ,並使用那些認證搭配 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

備註

方法會將 AddNetworkCredential 實例放在 SMTP CredentialCache以外的通訊協定中使用。 快取會依其新增順序來儲存認證。 GetCredential(Uri, String)呼叫 方法時,它會傳回適當的相符NetworkCredential實例。

適用於

Add(String, Int32, String, NetworkCredential)

來源:
CredentialCache.cs
來源:
CredentialCache.cs
來源:
CredentialCache.cs

將要搭配 Simple Mail Transfer Protocol (SMTP) 使用的 NetworkCredential 執行個體加入至認證快取,並使其與主機電腦、連接埠和驗證通訊協定產生關聯。 使用此方法加入的認證只適用於 SMTP。 這個方法不適用於 HTTP 或 FTP 要求。

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)

參數

host
String

識別主機電腦的 String

port
Int32

Int32,指定 host 上的連接埠。

authenticationType
String

String,識別使用 host 連接到 cred 時,所使用的驗證配置。

credential
NetworkCredential

NetworkCredential,要加入至認證快取。

例外狀況

hostnull

-或-

authTypenull

authType 不是接受的值。

port 小於零。

範例

下列程式代碼範例會 CredentialCache 使用多個安全性認證來初始化,以便與 SMTP 搭配使用,並使用其中一個認證搭配 SmtpClient

SmtpClient^ client = gcnew SmtpClient("ContosoMail", 45);
MailAddress^ from = gcnew MailAddress("sender@SenderMailServerName.com", "Sender Name");
MailAddress^ to = gcnew MailAddress("recepient@RecepientMailServerName.com", "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("sender@SenderMailServerName.com", "Sender Name");
MailAddress to = new MailAddress("recepient@RecepientMailServerName.com", "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("sender@SenderMailServerName.com", "Sender Name")
Dim sendTo As New MailAddress("recepient@RecepientMailServerName.com", "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

備註

這個方法會將 NetworkCredential 實例放在 中,以便與 SMTP CredentialCache搭配使用。 快取會依其新增順序來儲存認證。 GetCredential(String, Int32, String)呼叫 方法時,它會傳回NetworkCredential比對 、 portauthType所選取的host實例。 比較不區分大小寫。

的支援值為 authType “NTLM”、“Digest”、“Kerberos” 和 “Negotiate”。

使用這個方法新增的認證只適用於 SMTP。 這個方法不適用於 HTTP 或 FTP 通訊協定。

適用於