This documentation is archived and is not being maintained.

HttpWebRequest.Credentials Property

Gets or sets authentication information for the request.

Namespace:  System.Net
Assembly:  System (in System.dll)

public override function get Credentials () : ICredentials 
public override function set Credentials (value : ICredentials)

Property Value

Type: System.Net.ICredentials
An ICredentials that contains the authentication credentials associated with the request. The default is a null reference (Nothing in Visual Basic).

The Credentials property contains authentication information to identify the maker of the request. The Credentials property can be either a NetworkCredential, in which case the user, password, and domain information contained in the NetworkCredential object is used to authenticate the request, or it can be a CredentialCache, in which case the Uniform Resource Identifier (URI) of the request is used to determine the user, password, and domain information to use to authenticate the request.

In most client scenarios, you should use the DefaultCredentials property, which contains the credentials of the currently logged on user. To do this, set the UseDefaultCredentials property to true instead of setting this property.

If the HttpWebRequest class is being used in a middle-tier application, such as an ASP.NET application, the credentials in the DefaultCredentials property 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.


The NTLM authentication scheme cannot be used to impersonate another user. Kerberos must be specially configured to support impersonation.

To restrict HttpWebRequest to one or more authentication methods, use the CredentialCache class and bind your credentials to one or more authentication schemes

Supported authentication schemes include Digest, Negotiate, Kerberos, NTLM, and Basic.

The following code example sets the credentials for a request.

No code example is currently available or this language may not be supported.
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Net;
using namespace System::Text;
using namespace System::IO;

// Specify the URL to receive the request.
int main ()
    String* args[] = Environment::GetCommandLineArgs();

    HttpWebRequest* request = dynamic_cast<HttpWebRequest*>(WebRequest::Create (args[1]));

    // Set some reasonable limits on resources used by this request
    request->MaximumAutomaticRedirections = 4;
    request->MaximumResponseHeadersLength = 4;
    // Set credentials to use for this request.
    request->Credentials = CredentialCache::DefaultCredentials;
    HttpWebResponse* response = dynamic_cast<HttpWebResponse*>(request->GetResponse ());

    Console::WriteLine (S"Content length is {0}", __box(response->ContentLength));
    Console::WriteLine (S"Content type is {0}", response->ContentType);

    // Get the stream associated with the response.
    Stream* receiveStream = response->GetResponseStream ();

    // Pipes the stream to a higher level stream reader with the required encoding format. 
    StreamReader* readStream = new StreamReader (receiveStream, Encoding::UTF8);

    Console::WriteLine (S"Response stream received.");
    Console::WriteLine (readStream->ReadToEnd ());
    response->Close ();
    readStream->Close ();

The output from this example will vary depending on the value passed into Main 
but will be similar to the following:

Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0