Redigera

Dela via


WebClient.Credentials Property

Definition

Gets or sets the network credentials that are sent to the host and used to authenticate the request.

public:
 property System::Net::ICredentials ^ Credentials { System::Net::ICredentials ^ get(); void set(System::Net::ICredentials ^ value); };
public System.Net.ICredentials? Credentials { get; set; }
public System.Net.ICredentials Credentials { get; set; }
member this.Credentials : System.Net.ICredentials with get, set
Public Property Credentials As ICredentials

Property Value

An ICredentials containing the authentication credentials for the request. The default is null.

Examples

The following code example uses the user's system credentials to authenticate a request.

int main()
{
   try
   {
      WebClient^ client = gcnew WebClient;
      client->Credentials = CredentialCache::DefaultCredentials;
      array<Byte>^pageData = client->DownloadData( "http://www.contoso.com" );
      String^ pageHtml = Encoding::ASCII->GetString( pageData );
      Console::WriteLine( pageHtml );
   }
   catch ( WebException^ webEx ) 
   {
      Console::Write( webEx );
   }

}

public static void Main()
{           
    try {

        WebClient client = new WebClient();

        client.Credentials = CredentialCache.DefaultCredentials;

        Byte[] pageData = client.DownloadData("http://www.contoso.com");
        string pageHtml = Encoding.ASCII.GetString(pageData);
        Console.WriteLine(pageHtml);
    } catch (WebException webEx) {
        Console.Write(webEx.ToString());
    }
}    
Public Shared Sub Main()
    Try
        Dim client As New WebClient()

        client.Credentials = CredentialCache.DefaultCredentials

        Dim pageData As [Byte]() = client.DownloadData("http://www.contoso.com")
        Dim pageHtml As String = Encoding.ASCII.GetString(pageData)
        
        Console.WriteLine(pageHtml)

    Catch webEx As WebException
        Console.Write(webEx.ToString())
    End Try
End Sub 

Remarks

Caution

WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete, and you shouldn't use them for new development. Use HttpClient instead.

The Credentials property contains the authentication credentials used to access a resource on a host. In most client-side scenarios, you should use the DefaultCredentials, which are the credentials of the currently logged on user. To do this, set the UseDefaultCredentials property to true instead of setting this property.

If the WebClient class is being used in a middle tier application, such as an ASP.NET application, the DefaultCredentials belong to the account running the ASP page (the server-side credentials). Typically, you would set this property to the credentials of the client on whose behalf the request is made.

For security reasons, when automatically following redirects, store the credentials that you want to be included in the redirect in a CredentialCache and assign it to this property. This property will automatically be set to null upon redirection if it contains anything except a CredentialCache. Having this property value be automatically set to null under those conditions prevents credentials from being sent to any unintended destination.

Applies to

See also