共用方式為


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

備註

謹慎

WebRequestHttpWebRequestServicePointWebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient

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

適用於