System.Security.SecurityException: 不允许所请求的注册表访问权

当ASP.NET应用程序尝试在EventLog中创建新的EventSource时,可能会遇到'System.Security.SecurityException',提示'Requested Registry Access Is Not Allowed'。这个问题通常与权限不足或注册表访问限制有关。解决方案包括修改应用程序池标识或使用管理员权限运行应用。
 

PRB:ASP.NET 应用程序试图在 EventLog 中写入新的 EventSource 时出现“Requested Registry Access Is Not Allowed”(不允许所请求的注册表访问)错误信息

文章编号:329291
最后修改:2005年7月11日
修订:2.1
重要说明:本文包含有关修改注册表的信息。修改注册表之前,一定要备份注册表,并且一定要知道在发生问题时如何还原注册表。有关如何备份、还原和编辑注册表的信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
256986 (http://support.microsoft.com/kb/256986/EN-US/) Microsoft Windows 注册表说明

症状

使用 ASP.NET 在事件日志中创建一个新的事件源时,您可能会收到下面的错误信息:
System.Security.SecurityException:Requested registry access is not allowed.

原因

默认情况下,ASP.NET 工作进程的用户令牌是 ASPNET(或者,对于 Internet 信息服务 [IIS] 6.0 上运行的应用程序是 NetworkService)。由于您的帐户不具有创建事件源的正确用户权限,会出现“症状”部分中的问题。

解决方案

警告:注册表编辑器使用不当可导致严重问题,从而可能需要重新安装操作系统。Microsoft 不能保证您可以解决因注册表编辑器使用不当而导致的问题。使用注册表编辑器需要您自担风险。 要解决此问题,在您运行 ASP.NET Web 应用程序之前,拥有管理权限的用户必须创建事件源。要创建事件源,请使用下列方法之一。
第一种方法
在注册表编辑器中,在 应用程序事件日志下创建一个事件源。为此,请执行下列步骤:
1.单击“开始”,然后单击“运行”。
2.在“打开”文本框中,键入 regedit
3.找到以下注册表子项:
HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Eventlog/Application
4.右键单击“Application”子项,指向“新建”,然后单击“项”。
5.键入 TEST 作为该项的名称。
6.关闭注册表编辑器。
第二种方法
System.Diagnostics 名称空间中的 EventLogInstaller 类允许您安装和配置一个事件日志,您的应用程序在运行时可以读取或写入该事件日志。您可以使用 EventLogInstaller 创建一个事件源。为此,请执行下列步骤:
1.使用 Microsoft Visual Basic .NET 或 Microsoft Visual C# .NET 创建一个新的名为 EventLogSourceInstaller 的“类库”。默认情况下,将创建“Class1.vb”文件或“Class1.cs”文件。
2.在解决方案资源管理器中,右键单击“EventLogSourceInstaller”,然后单击“添加引用”。
3.在“添加引用”对话框中,双击“System.Configuration.Install.dll”,然后单击“确定”。
4.将 Class1.vb/Class1.cs 重命名为 MyEventLogInstaller.vb/MyEventLogInstaller.cs。
5.用以下示例代码替换 MyEventLogInstaller.vb 或 MyEventLogInstaller.cs 中现有的代码:

Visual Basic .NET 示例
Imports System.Diagnostics
Imports System.Configuration.Install
Imports System.ComponentModel

<RunInstaller(True)> _
Public Class MyEventLogInstaller
    Inherits Installer
    Private myEventLogInstaller As EventLogInstaller

    Public Sub New()
        ' Create an instance of 'EventLogInstaller'.
        myEventLogInstaller = New EventLogInstaller()
        ' Set the 'Source' of the event log, to be created.
        myEventLogInstaller.Source = "TEST"
        ' Set the 'Log' that the source is created in.
        myEventLogInstaller.Log = "Application"
        ' Add myEventLogInstaller to 'InstallerCollection'.
        Installers.Add(myEventLogInstaller)
    End Sub 
End Class 
Visual C# .NET 示例
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Configuration.Install;


namespace EventLogSourceInstaller 
{
	[RunInstaller(true)]
	public class MyEventLogInstaller : Installer
	{
		private EventLogInstaller myEventLogInstaller;

		public MyEventLogInstaller()
		{
			//Create Instance of EventLogInstaller
			myEventLogInstaller = new EventLogInstaller();

			// Set the Source of Event Log, to be created.
			myEventLogInstaller.Source = "TEST";

			// Set the Log that source is created in
			myEventLogInstaller.Log = "Application";
			
			// Add myEventLogInstaller to the Installers Collection.
			Installers.Add(myEventLogInstaller);
		}
	}
}

6.在“生成”菜单中,单击“生成解决方案”以生成“EventLogSourceInstaller.dll”。
7.打开“Visual Studio .NET 命令提示符”。
8.在该命令提示符下,更改到“EventLogSourceInstaller.dll”所在的文件夹。
9.运行以下命令以生成 EventSource:
InstallUtil EventLogSourceInstaller.dll

更多信息

重现此问题的步骤

1.使用 Visual Basic .NET 或 Visual C# .NET 创建一个新的“ASP.NET Web 应用程序”。默认情况下,将创建“WebForm1.aspx”文件。
2.在“WebForm1.aspx”的 HTML 视图中,用以下示例代码替换现有代码:

Visual Basic .NET 示例
<%@ Page Language="vb" AutoEventWireup="true" %>
<%@ Import namespace="System.Diagnostics" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<script language="VB" runat="server">
	Sub WriteEvent_Click(Src As Object, e As EventArgs)
	Dim ev As New EventLog("Application")
	' Event's Source name
	ev.Source = "TEST" 
	
	EventLog.CreateEventSource(ev.Source, "Application")
	
Try
	 ev.WriteEntry(TextBox1.Text)
	Catch b as exception
	 Response.write ("WriteEntry " & b.message & "<br>")
	End Try
	ev = Nothing
	End Sub
	</script>

	<body>
		<form id="Form1" runat="server">
			Event message: 
			<asp:textbox id="TextBox1" runat="server" Width="233px"></asp:textbox>
			<asp:button id="Button1" onclick="WriteEvent_Click" runat="server" NAME="Button1" text="Write to event log"></asp:button>
		</form>
	</body>
</HTML>
Visual C# .NET 示例
<%@ Page Language="c#" AutoEventWireup="true" %>
<%@ Import namespace="System.Diagnostics" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<script language="C#" runat="server">
	void WriteEvent_Click(Object Src, EventArgs e)
	{
	EventLog ev = new EventLog("Application");
	// Event's Source name
	ev.Source = "TEST";  
	
	EventLog.CreateEventSource(ev.Source, "Application");

			try
			{
				ev.WriteEntry(TextBox1.Text);
			}
			catch (Exception b)
			{
				Response.Write("WriteEntry " + b.Message + "<br>");
			}
			ev = null;
	}
	</script>

	<body>
		<form id="Form1" runat="server">
			Event message: 
			<asp:textbox id="TextBox1" runat="server" Width="233px"></asp:textbox>
			<asp:button id="Button1" onclick="WriteEvent_Click" runat="server" NAME="Button1" text="Write to event log"></asp:button>
		</form>
	</body>
</HTML>
3.在“调试”菜单上,单击“开始”以在浏览器中查看“WebForm1.aspx”页面。
4.在“TextBox”中键入一些文字,然后单击“写入事件日志”。
5.本文“症状”部分讨论的错误信息将会出现。
6.要解决此问题,请创建一个如“解决方案”部分所讨论的事件源,并在“WebForm1.aspx”中给下列代码添加注释:
EventLog.CreateEventSource(ev.Source, "Application")
7.重复步骤 3 和 4。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值