共用方式為


IntranetZoneCredentialPolicy.ShouldSendCredential 方法

定義

傳回 Boolean,指出是否將用戶端的認證和 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

將接收要求的 Uri

request
WebRequest

WebRequest,表示所要求的資源。

credential
NetworkCredential

如果此方法傳回 true,則 NetworkCredential 將與要求一起傳送。

authModule
IAuthenticationModule

將執行驗證的 IAuthenticationModule,如果需要驗證。

傳回

如果要求的資源與產生要求的用戶端在相同的網域,則為 true,否則為 false

實作

範例

下列程式代碼範例示範衍生自 IntranetZoneCredentialPolicy ,以允許針對使用安全超文本傳輸通訊協定的要求傳送認證, (HTTPS) 搭配基本身份驗證。 使用 HTTPS 和基本身份驗證時,會先加密用戶密碼,再透過網路傳送。

// 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 ,負責向伺服器執行驗證。 如果這個方法傳 false回 ,則 IAuthenticationModule 不會向伺服器驗證用戶端。

這個方法只會針對指定認證或使用 WebProxy 指定認證的 物件的要求呼叫。

適用於