แก้ไข

แชร์ผ่าน


ICredentialPolicy Interface

Definition

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

public interface class ICredentialPolicy
public interface ICredentialPolicy
type ICredentialPolicy = interface
Public Interface ICredentialPolicy
Derived

Examples

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

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;
   }
};
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;
    }
}

Remarks

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.

Note

ICredentialPolicy policies are invoked only if the WebRequest or the WebProxy that is associated with the request has credentials that are not null. 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.

Methods

ShouldSendCredential(Uri, WebRequest, NetworkCredential, IAuthenticationModule)

Returns a Boolean that indicates whether the client's credentials are sent with a resource request made using an instance of the WebRequest class.

Applies to