Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
ICredentialPolicy Interface

Defines the credential policy to be used for resource requests that are made using WebRequest and its derived classes.

Namespace:  System.Net
Assembly:  System (in System.dll)
Visual Basic (Declaration)
Public Interface ICredentialPolicy
Visual Basic (Usage)
Dim instance As ICredentialPolicy
C#
public interface ICredentialPolicy
Visual C++
public interface class ICredentialPolicy
JScript
public interface ICredentialPolicy

The credential policy determines whether to send credentials when sending a WebRequest for a network resource, such as the content of a Web page. If credentials are sent, servers that require client authentication can attempt to authenticate the client when the request is received instead of sending a response that indicates that the client's credentials are required. While this saves a round trip to the server, this performance gain must be balanced against the security risk inherent in sending credentials across the network. When the destination server does not require client authentication, it is best not to send credentials.

NoteNote:

ICredentialPolicy policies are invoked only if the WebRequest or the WebProxy that is associated with the request has credentials that are not nullNothingnullptra null reference (Nothing in Visual Basic). Setting this policy has no effect on requests that do not specify credentials.

Use the AuthenticationManager..::.CredentialPolicy property to set an ICredentialPolicy policy. The IAuthenticationModule that handles authentication for the request will invoke the ShouldSendCredential method before performing the authentication. If the method returns false, authentication is not performed.

An ICredentialPolicy policy affects all instances of WebRequest with non-null credentials in the current application domain. The policy cannot be overridden on individual requests.

The following code example shows an implementation of this interface that permits credentials to be sent only for requests that target specific hosts.

C#
public class SelectedHostsCredentialPolicy: ICredentialPolicy
{
    public SelectedHostsCredentialPolicy()
    {
    }

    public virtual bool ShouldSendCredential(Uri challengeUri, 
        WebRequest request, 
        NetworkCredential credential, 
        IAuthenticationModule authModule)
    {
        Console.WriteLine("Checking custom credential policy.");
        if (request.RequestUri.Host == "www.contoso.com" ||
            challengeUri.IsLoopback == true)
            return true;

        return false;
    }
}

Visual C++
public ref class SelectedHostsCredentialPolicy: public ICredentialPolicy
{
public:
   SelectedHostsCredentialPolicy(){}

   virtual bool ShouldSendCredential( Uri^ challengeUri, WebRequest^ request, NetworkCredential^ /*credential*/, IAuthenticationModule^ /*authModule*/ )
   {
      Console::WriteLine( L"Checking custom credential policy." );
      if ( request->RequestUri->Host->Equals( L"www.contoso.com" ) || challengeUri->IsLoopback == true )
            return true;

      return false;
   }
};

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

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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker