Share via


IntranetZoneCredentialPolicy.ShouldSendCredential Metode

Definisi

Boolean Mengembalikan yang menunjukkan apakah kredensial klien dikirim dengan permintaan sumber daya yang dibuat menggunakan 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

Parameter

challengeUri
Uri

Uri yang akan menerima permintaan.

request
WebRequest

WebRequest yang mewakili sumber daya yang diminta.

credential
NetworkCredential

NetworkCredential yang akan dikirim dengan permintaan jika metode ini mengembalikan true.

authModule
IAuthenticationModule

IAuthenticationModule yang akan melakukan autentikasi, jika autentikasi diperlukan.

Mengembalikan

true jika sumber daya yang diminta berada di domain yang sama dengan klien yang membuat permintaan; jika tidak, false.

Penerapan

Contoh

Contoh kode berikut menunjukkan berasal dari IntranetZoneCredentialPolicy untuk memungkinkan kredensial dikirim untuk permintaan yang menggunakan Secure Hypertext Transfer Protocol (HTTPS) dengan autentikasi dasar. Menggunakan HTTPS dan autentikasi dasar, kata sandi pengguna dienkripsi sebelum dikirim melalui jaringan.

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

Keterangan

Aplikasi tidak memanggil metode ini secara langsung; ini dipanggil oleh IAuthenticationModule yang bertanggung jawab untuk melakukan autentikasi dengan server. Jika metode ini mengembalikan false, IAuthenticationModule tidak akan mengautentikasi klien ke server.

Metode ini dipanggil hanya untuk permintaan yang menentukan kredensial atau menggunakan WebProxy objek yang menentukan kredensial.

Berlaku untuk