NetworkCredential 类

为基于密码的身份验证方案(如基本、简要、NTLM 和 Kerberos 身份验证)提供凭据。

**命名空间:**System.Net
**程序集:**System(在 system.dll 中)

语法

声明
Public Class NetworkCredential
    Implements ICredentials, ICredentialsByHost
用法
Dim instance As NetworkCredential
public class NetworkCredential : ICredentials, ICredentialsByHost
public ref class NetworkCredential : ICredentials, ICredentialsByHost
public class NetworkCredential implements ICredentials, ICredentialsByHost
public class NetworkCredential implements ICredentials, ICredentialsByHost

备注

NetworkCredential 类是为基于密码的身份验证方案(如基本、简要、NTLM 和 Kerberos)提供凭据的基类。实现 ICredentials 接口的类(如 CredentialCache 类)返回 NetworkCredential 对象。

此类不支持基于公钥的身份验证方法,如安全套接字层 (SSL) 客户端身份验证。

示例

下面的代码示例将 NetworkCredential 对象与 CredentialCache 中的一组统一资源标识符 (URI) 相关联。它随后将 CredentialCache 传递给 WebRequest 对象,后者使用此信息对向 Internet 服务器发出的请求进行身份验证。

Dim myCred As New NetworkCredential(SecurelyStoredUserName,SecurelyStoredPassword,SecurelyStoredDomain )
       
Dim myCache As New CredentialCache()
       
myCache.Add(New Uri("www.contoso.com"), "Basic", myCred)
myCache.Add(New Uri("app.contoso.com"), "Basic", myCred)
       
Dim wr As WebRequest = WebRequest.Create("www.contoso.com")
wr.Credentials = myCache
NetworkCredential myCred = new NetworkCredential(
    SecurelyStoredUserName,SecurelyStoredPassword,SecurelyStoredDomain);
 
CredentialCache myCache = new CredentialCache();
 
myCache.Add(new Uri("www.contoso.com"), "Basic", myCred);
myCache.Add(new Uri("app.contoso.com"), "Basic", myCred);
 
WebRequest wr = WebRequest.Create("www.contoso.com");
wr.Credentials = myCache;
NetworkCredential^ myCred = gcnew NetworkCredential(
   SecurelyStoredUserName,SecurelyStoredPassword,SecurelyStoredDomain );

CredentialCache^ myCache = gcnew CredentialCache;

myCache->Add( gcnew Uri( "www.contoso.com" ), "Basic", myCred );
myCache->Add( gcnew Uri( "app.contoso.com" ), "Basic", myCred );

WebRequest^ wr = WebRequest::Create( "www.contoso.com" );
wr->Credentials = myCache;
NetworkCredential myCred =new NetworkCredential(securelyStoredUserName,
    securelyStoredPassword,securelyStoredDomain);
CredentialCache myCache = new CredentialCache();

myCache.Add(new Uri("www.contoso.com"), "Basic", myCred);
myCache.Add(new Uri("app.contoso.com"), "Basic", myCred);

WebRequest wr = WebRequest.Create("www.contoso.com");
wr.set_Credentials(myCache);
var myCred : NetworkCredential = new NetworkCredential(
   SecurelyStoredUserName, SecurelyStoredPassword, SecurelyStoredDomain )
       
var myCache : CredentialCache = new CredentialCache()
       
myCache.Add(new Uri("www.contoso.com"), "Basic", myCred)
myCache.Add(new Uri("app.contoso.com"), "Basic", myCred)
       
var wr : WebRequest = WebRequest.Create("www.contoso.com")
wr.Credentials = myCache

继承层次结构

System.Object
  System.Net.NetworkCredential

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

NetworkCredential 成员
System.Net 命名空间