ICertificatePolicy 介面
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
驗證伺服器憑證。
public interface class ICertificatePolicy
public interface ICertificatePolicy
type ICertificatePolicy = interface
Public Interface ICertificatePolicy
範例
下列範例會建立憑證原則,針對任何憑證問題傳回 false
,並列印指出控制台上問題的訊息。 CertificateProblem 列舉會定義憑證問題的 SSPI 常數,而私人 GetProblemMessage 方法會建立有關問題的可列印訊息。
public enum class CertificateProblem : UInt32
{
CertEXPIRED = 0x800B0101,
CertVALIDITYPERIODNESTING = 0x800B0102,
CertROLE = 0x800B0103,
CertPATHLENCONST = 0x800B0104,
CertCRITICAL = 0x800B0105,
CertPURPOSE = 0x800B0106,
CertISSUERCHAINING = 0x800B0107,
CertMALFORMED = 0x800B0108,
CertUNTRUSTEDROOT = 0x800B0109,
CertCHAINING = 0x800B010A,
CertREVOKED = 0x800B010C,
CertUNTRUSTEDTESTROOT = 0x800B010D,
CertREVOCATION_FAILURE = 0x800B010E,
CertCN_NO_MATCH = 0x800B010F,
CertWRONG_USAGE = 0x800B0110,
CertUNTRUSTEDCA = 0x800B0112
};
public ref class MyCertificateValidation: public ICertificatePolicy
{
public:
// Default policy for certificate validation.
static bool DefaultValidate = false;
virtual bool CheckValidationResult( ServicePoint^ /*sp*/, X509Certificate^ /*cert*/, WebRequest^ request, int problem )
{
bool ValidationResult = false;
Console::WriteLine( "Certificate Problem with accessing {0}", request->RequestUri );
Console::Write( "Problem code 0x{0:X8},", (int)problem );
Console::WriteLine( GetProblemMessage( (CertificateProblem)problem ) );
ValidationResult = DefaultValidate;
return ValidationResult;
}
private:
String^ GetProblemMessage( CertificateProblem Problem )
{
String^ ProblemMessage = "";
CertificateProblem problemList = CertificateProblem( );
String^ ProblemCodeName = Enum::GetName( problemList.GetType(), Problem );
if ( ProblemCodeName != nullptr )
ProblemMessage = String::Concat( ProblemMessage, "-Certificateproblem:", ProblemCodeName );
else
ProblemMessage = "Unknown Certificate Problem";
return ProblemMessage;
}
};
public enum CertificateProblem : long
{
CertEXPIRED = 0x800B0101,
CertVALIDITYPERIODNESTING = 0x800B0102,
CertROLE = 0x800B0103,
CertPATHLENCONST = 0x800B0104,
CertCRITICAL = 0x800B0105,
CertPURPOSE = 0x800B0106,
CertISSUERCHAINING = 0x800B0107,
CertMALFORMED = 0x800B0108,
CertUNTRUSTEDROOT = 0x800B0109,
CertCHAINING = 0x800B010A,
CertREVOKED = 0x800B010C,
CertUNTRUSTEDTESTROOT = 0x800B010D,
CertREVOCATION_FAILURE = 0x800B010E,
CertCN_NO_MATCH = 0x800B010F,
CertWRONG_USAGE = 0x800B0110,
CertUNTRUSTEDCA = 0x800B0112
}
public class MyCertificateValidation : ICertificatePolicy
{
// Default policy for certificate validation.
public static bool DefaultValidate = false;
public bool CheckValidationResult(ServicePoint sp, X509Certificate cert,
WebRequest request, int problem)
{
bool ValidationResult=false;
Console.WriteLine("Certificate Problem with accessing " +
request.RequestUri);
Console.Write("Problem code 0x{0:X8},",(int)problem);
Console.WriteLine(GetProblemMessage((CertificateProblem)problem));
ValidationResult = DefaultValidate;
return ValidationResult;
}
private String GetProblemMessage(CertificateProblem Problem)
{
String ProblemMessage = "";
CertificateProblem problemList = new CertificateProblem();
String ProblemCodeName = Enum.GetName(problemList.GetType(),Problem);
if(ProblemCodeName != null)
ProblemMessage = ProblemMessage + "-Certificateproblem:" +
ProblemCodeName;
else
ProblemMessage = "Unknown Certificate Problem";
return ProblemMessage;
}
}
Public Enum CertificateProblem As Long
CertEXPIRED = 2148204801 ' 0x800B0101
CertVALIDITYPERIODNESTING = 2148204802 ' 0x800B0102
CertROLE = 2148204803 ' 0x800B0103
CertPATHLENCONST = 2148204804 ' 0x800B0104
CertCRITICAL = 2148204805 ' 0x800B0105
CertPURPOSE = 2148204806 ' 0x800B0106
CertISSUERCHAINING = 2148204807 ' 0x800B0107
CertMALFORMED = 2148204808 ' 0x800B0108
CertUNTRUSTEDROOT = 2148204809 ' 0x800B0109
CertCHAINING = 2148204810 ' 0x800B010A
CertREVOKED = 2148204812 ' 0x800B010C
CertUNTRUSTEDTESTROOT = 2148204813 ' 0x800B010D
CertREVOCATION_FAILURE = 2148204814 ' 0x800B010E
CertCN_NO_MATCH = 2148204815 ' 0x800B010F
CertWRONG_USAGE = 2148204816 ' 0x800B0110
CertUNTRUSTEDCA = 2148204818 ' 0x800B0112
End Enum
Public Class MyCertificateValidation
Implements ICertificatePolicy
' Default policy for certificate validation.
Public Shared DefaultValidate As Boolean = False
Public Function CheckValidationResult(srvPoint As ServicePoint, _
cert As X509Certificate, request As WebRequest, problem As Integer) _
As Boolean Implements ICertificatePolicy.CheckValidationResult
Dim ValidationResult As Boolean = False
Console.WriteLine(("Certificate Problem with accessing " & _
request.RequestUri.ToString()))
Console.Write("Problem code 0x{0:X8},", CInt(problem))
Console.WriteLine(GetProblemMessage(CType(problem, _
CertificateProblem)))
ValidationResult = DefaultValidate
Return ValidationResult
End Function
Private Function GetProblemMessage(Problem As CertificateProblem) As String
Dim ProblemMessage As String = ""
Dim problemList As New CertificateProblem()
Dim ProblemCodeName As String = System.Enum.GetName( _
problemList.GetType(), Problem)
If Not (ProblemCodeName Is Nothing) Then
ProblemMessage = ProblemMessage + "-Certificateproblem:" & _
ProblemCodeName
Else
ProblemMessage = "Unknown Certificate Problem"
End If
Return ProblemMessage
End Function
End Class
備註
介面 ICertificatePolicy 可用來為應用程式提供自定義安全性憑證驗證。 默認原則是允許有效的憑證,以及已過期的有效憑證。 若要變更此原則,請使用 ICertificatePolicy 不同的原則實作 介面,然後將該原則指派給 ServicePointManager.CertificatePolicy。
ICertificatePolicy 使用 SSPI (SSPI) 的安全性支援提供者介面。 如需詳細資訊,請參閱 MSDN 上的 SSPI 檔。
方法
CheckValidationResult(ServicePoint, X509Certificate, WebRequest, Int32) |
驗證伺服器憑證。 |