IntranetZoneCredentialPolicy.ShouldSendCredential 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳 a 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
參數
- request
- WebRequest
那 WebRequest 代表被請求的資源。
- credential
- NetworkCredential
NetworkCredential如果此方法返回 true,該請求會隨之傳送 。
- 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 )
{
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 )
{
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 指定憑證物件的請求時被呼叫。