我们在WEB站点使用企业库的时候,有时候会有如下的异常产生:
安全性异常
说明: 应用程序试图执行安全策略不允许的操作。要授予此应用程序所需的权限,请与系统管理员联系,或在配置文件中更改该应用程序的信任级别。
异常详细信息: System.Security.SecurityException: 不允许所请求的注册表访问权。
堆栈跟踪:
[SecurityException: 不允许所请求的注册表访问权。]
Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable) +473
System.Diagnostics.EventLog.CreateEventSource(String source, String logName, String machineName, Boolean useMutex) +443
System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) +347
System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category) +21
........
Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.PerformanceCounterInstances..ctor(String categoryName, String counterName, Boolean createNewInstance)
Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.InstrumentedEvent.AddPerformanceCounter(String category, String[] counterNames, Boolean createNewInstance) Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.InstrumentedEvent.Initialize(String counterCategory, String[] counterNames, Boolean createNewInstance, String eventLogSource, EventLogIdentifier[] eventIds)
造成这个的原因是企业库的公共项目中:Instrumentation 目录下有操作日志、性能的代码。而这些代码需要一定的权限才可以执行。
解决方案:
1、去掉这些代码。(这会是一个痛苦的经历,代码割舍不清呀)
2、给IUSR 帐户访问注册表的权限(安全问题呀!)
我们当然不推荐这2个做法:
我们推荐的做法:重新编译 Microsoft.Practices.EnterpriseLibrary.Common 项目,编译时把其中 Conditional Compilation Constants 设置中除 DEBUG;TRACE 外的其他项都去掉。
具体步骤请参看:
http://dotnetjunkies.com/WebLog/sskokku/archive/2005/03/25/61376.aspx
附图:
打印 | 张贴于 2005-07-26 17:03:00 | Tag:.net 编程心得 技术随笔
留言反馈
尝试创建自定义事件日志时,将会收到“Requested registry access is not allowed(不允许所请求的注册表访问权)”错误消息
http://support.microsoft.com/kb/842795/zh-cn
网址:www.wuqiannian.cn
E-mail:web@wuqiannian.cn
首先,不让虚拟目录启用匿名访问,这样在本机管理员身份下访问你的web应用,因为你当前的身份应该拥有对注册表的权限,所以肯定能做EntLib要做的事情。然后,再让虚拟目录启用匿名访问,再次访问Web应用,一切依旧ok,不再报告“System.Security.SecurityException: 不允许所请求的注册表访问权。”了。
{
totalEntriesInstances.RawValue(totalItems);
}这段代码出错!totalEntriesInstances是空的!我的email:hjf1223@163.com
DEBUG;TRACE;USEWMI;USEEVENTLOG;USEPERFORMANCECOUNTER
1. Open Start->programs->MS Patterns and Practices -> Ent Lib -> App Blocks -> Ent Lib.Sln.
2. Take your own sweet time to minimize all the folder(s) in ur solution window.
3. Rt Clk on the “Common” project and go to “Properties”
4. In the “Configuration Properties” go to the “Build” option.
5. In the Conditional Build section of the property grid, remove everything except “DEBUG;TRACE”
6. Close everything and recompile.
Remember...on compilation nothing will go to your common bin folder. I just plainly used the “Copy Assemblies to bin directory” option in my Start->programs->MS Patterns and Practices -> Ent Lib part of my menu.