Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

WebClient.Credentials Property

Gets or sets the network credentials used to authenticate the request with the Internet resource.

[Visual Basic]
Public Property Credentials As ICredentials
[C#]
public ICredentials Credentials {get; set;}
[C++]
public: __property ICredentials* get_Credentials();
public: __property void set_Credentials(ICredentials*);
[JScript]
public function get Credentials() : ICredentials;
public function set Credentials(ICredentials);

Property Value

An ICredentials containing the authentication credentials for the request. The default is a null reference (Nothing in Visual Basic).

Remarks

The Credentials property contains the authentication credentials required to access the Internet resource.

Example

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

[Visual Basic] 
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 

[C#] 
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());
    }
}    

[C++] 
int main()
{
   try
   {
      WebClient* client = new WebClient();
      client -> Credentials = CredentialCache::DefaultCredentials;
      Byte pageData[] = client -> DownloadData(S"http://www.contoso.com");
      String* pageHtml = Encoding::ASCII -> GetString(pageData);
      Console::WriteLine(pageHtml);

   }
   catch (WebException* webEx)
   {
      Console::Write(webEx);
   }
}    

[JScript] 
public static function Main()
{           
    try {

        var client : WebClient = new WebClient();

        client.Credentials = CredentialCache.DefaultCredentials;

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

    } catch (webEx : WebException) {
        Console.Write(webEx.ToString());
    }
}    

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

WebClient Class | WebClient Members | System.Net Namespace | NetworkCredential | CredentialCache | WebRequest.Credentials

Show:
© 2014 Microsoft. All rights reserved.