2025-03-28 09:49:56 +08:00

192 lines
6.9 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security;
using System.Runtime.InteropServices;
using System.Reflection;
using mshtml;
using SuperMap.RealEstate.Utility;
namespace SuperMap.RealEstate.SecurityDevice
{
[SecuritySafeCritical]
[Guid("104E3ED8-52A2-4768-8681-9E23094FD9AF"), ProgId("SuperMap.RealEstate.SecurityDevice.SecurityDeviceActiveX"), ComVisible(true)]
public partial class SecurityDeviceActiveX : UserControl, IObjectSafety
{
public SecurityDeviceActiveX()
{
InitializeComponent();
}
private string _ClientScript_Insert = "RealEstate.SecurityDevice.Insert";
private string _ClientScript_Remove = "RealEstate.SecurityDevice.Remove";
private string _ClientScript_Message = "RealEstate.SecurityDevice.ShowMessage";
private void securityDeviceControl_DeviceChanged(object sender, ClientSecurityDeviceEventArgs e)
{
try
{
if (DesignMode)
return;
if (e.DeviceEnum == ClientSecurityDeviceState.Insert)
{
string _key = string.Empty;// e.KeyInfo.Name + "|" + e.KeyInfo.User + "|" + StringHelper.Encrypt(e.KeyInfo.Key) + "|" + e.KeyInfo.Date + "|" + e.KeyInfo.Url;
//兼容老证书逻辑
try
{
_key = e.KeyInfo.Key.ToDecrypt();
_key = e.KeyInfo.Name + "|" + e.KeyInfo.User + "|" + e.KeyInfo.Key + "|" + e.KeyInfo.Date + "|" + e.KeyInfo.Url;
}
catch
{
_key = e.KeyInfo.Name + "|" + e.KeyInfo.User + "|" + e.KeyInfo.Key.ToEncrypt() + "|" + e.KeyInfo.Date + "|" + e.KeyInfo.Url;
}
finally
{
InsertSecurityDevice(_key);
}
}
else
{
RemoveSecurityDevice("");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
}
void InsertSecurityDevice(string value)
{
ExecScript(_ClientScript_Insert, value);
}
void RemoveSecurityDevice(string value)
{
ExecScript(_ClientScript_Remove, value);
}
private void ExecScript(string functionName, string functionParams)
{
if ((_IHTMLWindow2 != null) && (!_IHTMLWindow2.closed))
{
_IHTMLWindow2.execScript(functionName + "('" + functionParams + "');", "javascript");
}
}
/// <summary>
/// HTMLWindow2Class对象
/// </summary>
private mshtml.IHTMLWindow2 _IHTMLWindow2 = null;
/// <summary>
/// 将window对象传递进来
/// </summary>
/// <param name="obj">The obj.</param>
public bool Connect(object obj, bool showDeviceMessage)//, string insertScript,string removeScript)
{
_IHTMLWindow2 = obj as mshtml.IHTMLWindow2;
if (showDeviceMessage)
securityDeviceControl1.DeviceMessageChanged += new ClientSecurityDeviceMessageHandler(securityDeviceControl1_DeviceMessageChanged);
return securityDeviceControl1.Device_Connect();
}
void securityDeviceControl1_DeviceMessageChanged(object sender, ClientSecurityDeviceMessageEventArgs e)
{
ExecScript(_ClientScript_Message, e.Message);
}
#region IObjectSafety
private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";
private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";
private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";
private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";
private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";
private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
private const int S_OK = 0;
private const int E_FAIL = unchecked((int)0x80004005);
private const int E_NOINTERFACE = unchecked((int)0x80004002);
private bool _fSafeForScripting = true;
private bool _fSafeForInitializing = true;
public int GetInterfaceSafetyOptions(ref Guid riid,
ref int pdwSupportedOptions,
ref int pdwEnabledOptions)
{
int Rslt = E_FAIL;
string strGUID = riid.ToString("B");
pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
switch (strGUID)
{
case _IID_IDispatch:
case _IID_IDispatchEx:
Rslt = S_OK;
pdwEnabledOptions = 0;
if (_fSafeForScripting == true)
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
break;
case _IID_IPersistStorage:
case _IID_IPersistStream:
case _IID_IPersistPropertyBag:
Rslt = S_OK;
pdwEnabledOptions = 0;
if (_fSafeForInitializing == true)
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
break;
default:
Rslt = E_NOINTERFACE;
break;
}
return Rslt;
}
public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
{
int Rslt = E_FAIL;
string strGUID = riid.ToString("B");
switch (strGUID)
{
case _IID_IDispatch:
case _IID_IDispatchEx:
if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) &&
(_fSafeForScripting == true))
Rslt = S_OK;
break;
case _IID_IPersistStorage:
case _IID_IPersistStream:
case _IID_IPersistPropertyBag:
if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) &&
(_fSafeForInitializing == true))
Rslt = S_OK;
break;
default:
Rslt = E_NOINTERFACE;
break;
}
return Rslt;
}
#endregion
}
}