CredentialCache.Add 方法

定义

向凭据缓存添加 NetworkCredential 实例。Adds a NetworkCredential instance to the credential cache.

重载

Add(Uri, String, NetworkCredential)

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

向凭据缓存添加要与 SMTP 一起使用的 NetworkCredential 实例,并将其与主机、端口和身份验证协议关联。Adds a NetworkCredential instance for use with SMTP to the credential cache and associates it with a host computer, port, and authentication protocol. 使用此方法添加的凭据仅对 SMTP 有效。Credentials added using this method are valid for SMTP only. 此方法对 HTTP 或 FTP 请求无效。This method does not work for HTTP or FTP requests.

Add(Uri, String, NetworkCredential)

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

参数

uriPrefix
Uri

一个 Uri,它指定资源的 URI 前缀,该资源由凭据授予访问权。A Uri that specifies the URI prefix of the resources that the credential grants access to.

authType
String

uriPrefix 中命名的资源所使用的身份验证方案。The authentication scheme used by the resource named in uriPrefix.

cred
NetworkCredential

要添加到凭据缓存中的 NetworkCredentialThe NetworkCredential to add to the credential cache.

例外

uriPrefixnulluriPrefix is null.

-or- authTypenullauthType is null.

相同的凭据被添加多次。The same credentials are added more than once.

示例

下面的代码示例使用多个安全凭据初始化 CredentialCache,并将这些凭据用于 WebRequestThe 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

注解

Add 方法将与 SMTP 之外的协议一起使用的 NetworkCredential 实例置于 CredentialCache中。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. 调用 GetCredential(Uri, String) 方法时,它将返回正确的匹配 NetworkCredential 实例。When the GetCredential(Uri, String) method is called, it returns the proper matching NetworkCredential instance.

Add(String, Int32, String, NetworkCredential)

向凭据缓存添加要与 SMTP 一起使用的 NetworkCredential 实例,并将其与主机、端口和身份验证协议关联。Adds a NetworkCredential instance for use with SMTP to the credential cache and associates it with a host computer, port, and authentication protocol. 使用此方法添加的凭据仅对 SMTP 有效。Credentials added using this method are valid for SMTP only. 此方法对 HTTP 或 FTP 请求无效。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)

参数

host
String

标识主机的 StringA String that identifies the host computer.

port
Int32

指定要连接到 Int32 的端口的 hostA Int32 that specifies the port to connect to on host.

authenticationType
String

String,它标识使用 host 连接到 cred 时使用的身份验证方案。A String that identifies the authentication scheme used when connecting to host using cred.

credential
NetworkCredential

要添加到凭据缓存中的 NetworkCredentialThe NetworkCredential to add to the credential cache.

例外

hostnullhost is null.

-or- authTypenullauthType is null.

authType 不是一个接受的值。authType not an accepted value.

port 小于零。port is less than zero.

示例

下面的代码示例使用多个安全凭据来初始化一个 CredentialCache 以与 SMTP 一起使用,并将其中一个凭据用于 SmtpClientThe 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("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 的 NetworkCredential 实例放置到 CredentialCache中。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. 调用 GetCredential(String, Int32, String) 方法时,它将返回通过匹配 hostportauthType选择的 NetworkCredential 实例。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.

authType 支持的值为 "NTLM"、"Digest"、"Kerberos" 和 "Negotiate"。The supported values for authType are "NTLM", "Digest", "Kerberos", and "Negotiate".

使用此方法添加的凭据仅适用于 SMTP。Credentials added with this method are only valid for use with SMTP. 此方法对 HTTP 或 FTP 协议不起作用。This method does not work for HTTP or FTP protocols.

适用于