问题场景
在写一个读写数据库的WinForm组件,当该组件在web页面上引用的时候,出现错误:
请求“System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”类型的权限已失败。”
运行环境为: IIS5.1 + ASP.NET4.0 + WinXP
解决方法
参考如下文档,解决我的问题
The error 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failedgenerally occurs when try to run the stored procedure.
This error often occurs when try to run the Stored Procedure from the web application.
Possible Reason
1. Not enough security permission for the stored Procedure
2. Anything that access database from SP requires at least the WSS_Medium security policy in the web.config file.
3. If the security message is from the web part, it's usually the trust element in the web.config file.
How to Fix?
1. Open wss_mediumtrust.config & wss_minimaltrust.config files
2. The normal path is (C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/12/config/)
3. We can find the exact path in web.config file
4. Find in the wss_mediumtrust.config File:
Name="SqlClientPermission"
Description="System.Data.SqlClient.SqlClientPermis sion, System.Data,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
5. Copy and paste it in to the node of wss_minimaltrust.config file.
6. In the PermissionSet section of this configuration file, add the following:
Find in wss_mediumtrust.config: class="SqlClientPermission" version="1" Unrestricted="true"/>
Copy and paste it in to the a node of wss_minimaltrust.config.
7. Goto Control Panel - Administrative Tools
8. Goto .NET Framework configuration settings
9. 设置步骤如下:
a. Expand the runtime security policy.
b. expand machine
c. expand code groups
d. right click all_code
e. Click on properties
f. Click on permission tab
g. Set Modified nothing to Full Trust
总结:
不知道是版本的问题还是什么问题,对于上面的解决方案中的,步骤1-5中的文件wss_mediumtrust.config & wss_minimaltrust.config 我都没有找到,只执行了步骤7-9过程。另外配置的过程中,本机器有两个.net版本1.1和2.0的版本,我两个都设置了一下就OK了,有空再确认一下是1.1还是2.0版本造成的。
参考:
http://hi.baidu.com/gzqsd/blog/item/3ca125fad1b1c22a4e4aea68.html
http://hi.baidu.com/%D2%BB%B5%E3%C7%E7/blog/item/ac65af254f747420d4074289.html