CredentialCache.Add 方法

定义

向凭据缓存添加 NetworkCredential 实例。

重载

Add(Uri, String, NetworkCredential)

向凭据缓存添加一个要与 SMTP 以外的协议一起使用的 NetworkCredential 实例,并将其与统一资源标识符 (URI) 前缀和身份验证协议关联。

Add(String, Int32, String, NetworkCredential)

向凭据缓存添加要与 SMTP 一起使用的 NetworkCredential 实例,并将其与主机、端口和身份验证协议关联。 使用此方法添加的凭据仅对 SMTP 有效。 此方法对 HTTP 或 FTP 请求无效。

Add(Uri, String, NetworkCredential)

Source:
CredentialCache.cs
Source:
CredentialCache.cs
Source:
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

注解

方法 Add 将用于 SMTP 以外的协议的实例放入 NetworkCredentialCredentialCache。 缓存按凭据添加到它的顺序存储凭据。 GetCredential(Uri, String)调用 方法时,它将返回正确的匹配NetworkCredential实例。

适用于

Add(String, Int32, String, NetworkCredential)

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

向凭据缓存添加要与 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

指定要连接到 host 的端口的 Int32

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

注解

此方法将用于 SMTP 的实例放入 NetworkCredentialCredentialCache。 缓存按凭据添加到它的顺序存储凭据。 GetCredential(String, Int32, String)调用 方法时,它将返回通过NetworkCredential匹配 hostportauthType选择的实例。 比较不区分大小写。

支持的值为 authType “NTLM”、“Digest”、“Kerberos”和“Negotiate”。

使用此方法添加的凭据仅对 SMTP 有效。 此方法不适用于 HTTP 或 FTP 协议。

适用于