WebClient.UseDefaultCredentials 属性

定义

获取或设置 Boolean 值,该值控制 DefaultCredentials 是否随请求一起发送。

public:
 property bool UseDefaultCredentials { bool get(); void set(bool value); };
public bool UseDefaultCredentials { get; set; }
member this.UseDefaultCredentials : bool with get, set
Public Property UseDefaultCredentials As Boolean

属性值

如果使用默认凭据,则为 true;否则为 false。 默认值为 false

示例

下面的代码示例演示如何设置此属性。

// Sample call: UploadFileInBackground3("http://www.contoso.com/fileUpload.aspx", "data.txt")
void UploadFileInBackground3( String^ address, String^ fileName )
{

   WebClient^ client = gcnew WebClient;
   Uri ^uri = gcnew Uri(address);

   client->UseDefaultCredentials = true;

   client->UploadFileCompleted += gcnew UploadFileCompletedEventHandler( UploadFileCallback2 );
   client->UploadFileAsync( uri, fileName );
   Console::WriteLine( "File upload started." );
}

// Sample call: UploadFileInBackground3("http://www.contoso.com/fileUpload.aspx", "data.txt")
public static void UploadFileInBackground3(string address, string fileName)
{
    WebClient client = new WebClient();

    Uri uri = new Uri(address);

    client.UseDefaultCredentials = true;
    client.UploadFileCompleted += new UploadFileCompletedEventHandler(UploadFileCallback2);
    client.UploadFileAsync(uri, fileName);
    Console.WriteLine("File upload started.");
}
'  Sample call: UploadFileInBackground3("http:' www.contoso.com/fileUpload.aspx", "data.txt")
Public Shared Sub UploadFileInBackground3(ByVal address As String, ByVal fileName As String)

    Dim client As WebClient = New WebClient()
                Dim uri as Uri =  New Uri(address)
    client.UseDefaultCredentials = True
    AddHandler client.UploadFileCompleted, AddressOf UploadFileCallback2
    client.UploadFileAsync(uri, fileName)
    Console.WriteLine("File upload started.")
End Sub

注解

当此WebClient对象发出的请求应使用当前登录用户的默认凭据进行身份验证(如果服务器请求)时,请将此属性true设置为 。 对于客户端应用程序,这是在大多数情况下所需的行为。 对于中间层应用程序(例如 ASP.NET 应用程序),通常将 属性设置为 Credentials 代表其发出请求的客户端的凭据,而不是使用此属性。

适用于