HttpWebRequest.Credentials Property

Definition

Gets or sets authentication information for the request.

C#
public override System.Net.ICredentials Credentials { get; set; }
C#
public override System.Net.ICredentials? Credentials { get; set; }

Property Value

An ICredentials that contains the authentication credentials associated with the request. The default is null.

Examples

The following code example sets the credentials for a request.

C#
using System;
using System.Net;
using System.Text;
using System.IO;

    public class Test
    {
        // Specify the URL to receive the request.
        public static void Main (string[] args)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);

            // 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 = (HttpWebResponse)request.GetResponse();

            Console.WriteLine("Content length is {0}", response.ContentLength);
            Console.WriteLine("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("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.
<html>
...
</html>

*/

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 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.

Note

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.

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

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0