(二)通過IE安全控件認證
如果客戶機的IE未開啟訪問非安全標記的ActiveX控件,通過IE浏覽上面的步驟開發出的ActiveX控件 ,發現IE會給出警告:
此頁上的 ActiveX 對象可能不安全的。 要允許它將初始化並通過腳本訪問嗎?
或禁止訪問。這是客戶機IE的安全規則設置的,我們應該在控件開發上解決IE安全認證的問題。首先 我們要了解IE是如何判斷一個ActiveX控件是不安全的,參見Microsoft幫助和支持文檔:
How Internet Explorer Determines If ActiveX Controls Are Safe
There are two ways to mark a control as safe for scripting and initialization:
1.Implement the IObjectSafety interface.
2.Provide the following registry keys for the control's CLSID under the Implemented CategorIEs section:
a.The following key marks the control safe for scripting:
{7DD95801-9882-11CF-9FA9-00AA006C42C4}
b.The following key marks the control safe for initialization from persistent data:
{7DD95802-9882-11CF-9FA9-00AA006C42C4}
Microsoft recommends that you implement IObjectSafety to mark a control as safe or unsafe. This prevents other users from repackaging your control and marking it as safe when it is not.
我決定實現IObjectSafety接口來向IE表明ActiveX控件的安全標識,以保證控件再次打包時安全標識 不會被被改寫。
IObjectSafety是一個COM下的接口,對於C++程序來說,只需要實現它就行了,而.Net之下沒有這個接 口,在這種情況下,我們的ActiveX控件就是一個不帶類型庫的COM組件,必須使用C#代碼重新定義COM接 口。
這裡需要了解一點COM的接口知識。接口是COM的核心,它區分了在客戶和對象之間使用的契約和實現 。COM的接口有三種類型:定制接口÷分派接口和雙重接口。.Net Framework使用ComInterfaceType對它 進行了重定義:
namespace System.Runtime.InteropServices
{
// 摘要:
// IdentifIEs how to expose an interface to COM.
[Serializable]
[ComVisible(true)]
public enum ComInterfaceType
{
// 摘要:
// Indicates the interface is exposed to COM as a dual interface, which enables
// both early and late binding. System.Runtime.InteropServices.ComInterfaceType.InterfaceIsDual
// is the default value.
InterfaceIsDual = 0,
//
// 摘要:
// Indicates an interface is exposed to COM as an IUnknown -derived interface,
// which enables only early binding.
InterfaceIsIUnknown = 1,
//
// 摘要:
// Indicates an interface is exposed to COM as a dispinterface, which enables
// late binding only.
InterfaceIsIDispatch = 2,
}
}