IntranetZoneCredentialPolicy.ShouldSendCredential(Uri, WebRequest, NetworkCredential, IAuthenticationModule) 方法

定义

返回一个 Boolean,该值指示客户端的凭据是否随使用 WebRequest 发出的资源请求一起发送。Returns a Boolean that indicates whether the client's credentials are sent with a request for a resource that was made using WebRequest.

public:
 virtual bool ShouldSendCredential(Uri ^ challengeUri, System::Net::WebRequest ^ request, System::Net::NetworkCredential ^ credential, System::Net::IAuthenticationModule ^ authModule);
public virtual bool ShouldSendCredential (Uri challengeUri, System.Net.WebRequest request, System.Net.NetworkCredential credential, System.Net.IAuthenticationModule authModule);
abstract member ShouldSendCredential : Uri * System.Net.WebRequest * System.Net.NetworkCredential * System.Net.IAuthenticationModule -> bool
override this.ShouldSendCredential : Uri * System.Net.WebRequest * System.Net.NetworkCredential * System.Net.IAuthenticationModule -> bool
Public Overridable Function ShouldSendCredential (challengeUri As Uri, request As WebRequest, credential As NetworkCredential, authModule As IAuthenticationModule) As Boolean

参数

challengeUri
Uri

将接收该请求的 UriThe Uri that will receive the request.

request
WebRequest

表示被请求的资源的 WebRequestThe WebRequest that represents the resource being requested.

credential
NetworkCredential

将随请求一起发送的 NetworkCredential(如果此方法返回 true)。The NetworkCredential that will be sent with the request if this method returns true.

authModule
IAuthenticationModule

将执行身份验证的 IAuthenticationModule(如果要求身份验证)。The IAuthenticationModule that will conduct the authentication, if authentication is required.

返回

如果所请求的资源与发出请求的客户端在同一个域中,则为 true;否则为 falsetrue if the requested resource is in the same domain as the client making the request; otherwise, false.

实现

示例

下面的代码示例演示如何从IntranetZoneCredentialPolicy派生, 以允许为使用安全超文本传输协议 (HTTPS) 和基本身份验证的请求发送凭据。The following code example demonstrates deriving from IntranetZoneCredentialPolicy to allow credentials to be sent for requests that use Secure Hypertext Transfer Protocol (HTTPS) with basic authentication. 使用 HTTPS 和基本身份验证, 在通过网络发送用户密码之前对其进行加密。Using HTTPS and basic authentication, the user password is encrypted before being sent over the network.

// The following class allows credentials to be sent if they are for requests for resources
// in the same domain, or if the request uses the HTTPSscheme and basic authentication is 
// required.
public ref class HttpsBasicCredentialPolicy: public IntranetZoneCredentialPolicy
{
public:
   HttpsBasicCredentialPolicy(){}

   virtual bool ShouldSendCredential( Uri^ challengeUri, WebRequest^ request, NetworkCredential^ credential, IAuthenticationModule^ authModule ) override
   {
      Console::WriteLine( L"Checking custom credential policy for HTTPS and basic." );
      bool answer = IntranetZoneCredentialPolicy::ShouldSendCredential( challengeUri, request, credential, authModule );
      if ( answer == true )
      {
         Console::WriteLine( L"Sending credential for intranet resource." );
         return answer;
      }

      // Determine whether the base implementation returned false for basic and HTTPS.
      if ( request->RequestUri->Scheme == Uri::UriSchemeHttps && authModule->AuthenticationType->Equals( L"Basic" ) )
      {
         Console::WriteLine( L"Sending credential for HTTPS and basic." );
         return true;
      }

      return false;
   }
};
// The following class allows credentials to be sent if they are for requests for resources
// in the same domain, or if the request uses the HTTPSscheme and basic authentication is 
// required.

       public class HttpsBasicCredentialPolicy: IntranetZoneCredentialPolicy
    {
        public HttpsBasicCredentialPolicy()
        {
        }
        
        public override bool ShouldSendCredential(Uri challengeUri, 
            WebRequest request, 
            NetworkCredential credential, 
            IAuthenticationModule authModule)
        {
            Console.WriteLine("Checking custom credential policy for HTTPS and basic.");
            bool answer = base.ShouldSendCredential(challengeUri, request, credential, authModule);

            if (answer == true)
            {
                Console.WriteLine("Sending credential for intranet resource.");
                return answer;
            }
            // Determine whether the base implementation returned false for basic and HTTPS.
            if (request.RequestUri.Scheme == Uri.UriSchemeHttps &&
                authModule.AuthenticationType == "Basic")
            {
                Console.WriteLine("Sending credential for HTTPS and basic.");
                return true;
            }
            return false;
        }
    }

注解

应用程序不直接调用此方法;它由IAuthenticationModule负责对服务器执行身份验证的进行调用。Applications do not call this method directly; it is called by the IAuthenticationModule that is responsible for performing authentication with the server. 如果此方法返回false IAuthenticationModule , 则不会对服务器的客户端进行身份验证。If this method returns false, the IAuthenticationModule will not authenticate the client to the server.

仅对指定凭据或使用WebProxy指定凭据的对象的请求调用此方法。This method is called only for requests that specify credentials or use a WebProxy object that specifies credentials.

适用于