In fact, if we do not set, just modify the code, run the program, its error interface as shown below1As shown:
Figure 1
Throw an exception as follows:
Exception Text **************
System.MethodAccessException:Attempt by security transparent method ' Rare.Card.Libary.Controls.
Readcardcontrol.btnread_click (System.Object, System.EventArgs) ' To call native code through method ' Rare.Card.Libary.MifareOneHelper.rf_read (Int32, Int32, byte[]) ' failed. Methods must be security critical or
Security safe-critical to call native code.
By consulting MSDN, the exception is interpreted as follows:
The Common Language Runtime (CLR) security model has changed a lot in Microsoft. NET Framework 4. One such change is the adoption of Level2 transparency
( very similar to the Silverlight security model) is likely to affect the author of the AllowPartiallyTrustedCallers (APTCA) library. There are three kinds of transparency properties: SecurityTransparent, SecuritySafeCritical, and SecurityCritical.
SecurityTransparent: Code marked as SecurityTransparent is reliable from a security standpoint. It does not perform any dangerous actions, such as declaring permissions,
Execute unverifiable code or invoke native code. Nor can it directly invoke SecurityCritical code.
As noted above, all partially trusted code is enforced as securitytransparent for security reasons. This is also the default transparency of the APTCA library.
SecurityCritical: Unlike SecurityTransparent , securitycritical code can perform any required action. It can execute statements,
Invoke native code and other operations. It can invoke other methods and is not limited by the transparency token.
Only fully trusted code can be securitycritical. In fact, (non-APTCA) fully trusted code belongs to SecurityCritical by default.
Thus protecting them from the calls of transparent, partially trusted callers.
SecuritySafeCritical: The securitysafecritical code acts as a bridge, allowing transparent code to call critical methods. SecuritySafeCritical
the code has the same permissions as the SecurityCritical code, but it can be called by the SecurityTransparent code. Therefore, the SecuritySafeCritical code must expose the underlying SecurityCritical method in a secure manner (to avoid some partially trusted malicious code trying to pass through SecuritySafeCritical Layer attacks these methods), this is extremely important.
As with SecurityCritical code, securitysafecritical code must be fully trusted.
Specific can refer to:
Http://msdn.microsoft.com/zh-cn/magazine/ee336023.aspx
as explained by MSDN, the problem is that the C # class library Cardreader.library, which encapsulates the original DLL , lets you set the transparency properties at the code level to solve the problem.
Specific solutions are as follows:
1. Set the transparency property of the ActiveX control read-card code: securitysafecritical, set the following code list as follows:
Copy Code code as follows:
[SecuritySafeCritical]
<summary>
Read Card
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Btnread_click (object sender, EventArgs e)
{
int i = 0;
byte[] data = new BYTE[16];
byte[] buff = new BYTE[32];
for (i = 0; i < i++)
Data[i] = 0;
for (i = 0; i < i++)
Buff[i] = 0;
st = Mifareonehelper.rf_read (Icdev, SEC * 4 + 1, data);
if (st = = 0)
{
Serialinterfacehelper.hex_a (data, Buff, 16);
Txtcardid.text = System.Text.Encoding.ASCII.GetString (buff);
Lblmsg.text = "Read card number successful!";
}
Else
Lblmsg.text = "Read card number failed!";
Test method
if (string. IsNullOrEmpty (Txtcardid.text))
//{
Lblmsg.text = "Read data failed!" ";
//}
Else
//{
Lblmsg.text = string. Format (Read data: {0}! ", Txtcardid.text);
//}
}
Note To add a reference: using System.Security;
Here the test code is dropped, using serial communication and reading card code.
2. Set the transparent properties that encapsulate the original reader DLL.
Set the transparency property of the M1 reader Help class mifareonehelper to: [SecurityCritical], while setting the method of the call Mifareonehelper.rf_read the
The Transparent property is [SecurityCritical].
Set serial communication Help class Serialinterfacehelper's Transparent property is: [SecurityCritical], while setting the method of the call Serialinterfacehelper.hex_a
The Transparent property is [SecurityCritical].
The complete code has been provided, and 2 more places to note is that if the client fails to install ActiveX, the address of the running ActiveX is added to the trust site.
The trust site's security level is minimized or the option to trust the site about ActiveX is set.
Package Download Address Http://xiazai.jb51.net/201105/yuanma/CardReader.rar