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

備註

如果伺服器要求,應該使用目前登入使用者的預設認證進行驗證,請將這個屬性 trueWebClient 設定為 。 對於用戶端應用程式,這是大部分案例中所需的行為。 對於仲介層應用程式,例如 ASP.NET 應用程式,而不是使用此屬性,您通常會將 屬性設定 Credentials 為代表要求之用戶端的認證。

適用於