上一篇:在WEB窗体中如何转换页面和结束程序? >>
关于ASP.Net写注册表权限问题的官方解决方法
PRB: "Requested Registry Access Is Not Allowed" Error Message When ASP.NET Application Tries to Write New EventSource in the EventLog
The information in this article applies to:- Microsoft ASP.NET (included with the .NET Framework) 1.0
- Microsoft Visual Basic .NET (2002)
- Microsoft Visual C# .NET (2002)
256986 Description of the Microsoft Windows Registry
SYMPTOMS
When you create a new event source in the event log by using ASP.NET, you may receive the following error message:System.Security.SecurityException: Requested registry access is not allowed.
CAUSE
By default, the user token of the ASP.NET worker process is ASPNET. The problem in the "Symptoms" section occurs because your account does not have the correct user rights to create an event source.RESOLUTION
WARNING: If you use Registry Editor incorrectly, you may cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that you can solve problems that result from using Registry Editor incorrectly. Use Registry Editor at your own risk. To resolve this problem, a user who has administrative rights must create the event source before you run the ASP.NET Web Application. To create an event source, use one of the following approaches.First Approach
Create an event source under the Application event log in Registry Editor. To do this, follow these steps:- Click Start, and then click Run.
- In the Open text box, type regedit.
- Locate the following registry subkey:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application - Right-click the Application subkey, point to New, and then click Key.
- Type TEST for the key name.
- Close Registry Editor.
Second Approach
The EventLogInstaller class in the System.Diagnostics namespace permits you to install and configure an event log that your application reads from or writes to while running. You can create an event source by using EventLogInstaller. To do this, follow these steps:- Use Microsoft Visual Basic .NET or Microsoft Visual C# .NET to create a new Class Library named EventLogSourceInstaller. By default, the Class1.vb file or the Class1.cs file is created.
- In Solution Explorer, right-click EventLogSourceInstaller, and then click Add References.
- In the Add Reference dialog box, double-click System.Configuration.Install.dll, and then click OK.
- Rename the Class1.vb\Class1.cs to MyEventLogInstaller.vb\MyEventLogInstaller.cs.
- Replace the existing code in MyEventLogInstaller.vb or MyEventLogInstaller.cs with the following sample code:
Visual Basic .NET SampleImports System.DiagnosticsImports System.Configuration.InstallImports 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 Sampleusing 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); } }} - On the Build menu, click Build Solution to create EventLogSourceInstaller.dll.
- Open the Visual Studio .NET Command Prompt.
- At the command prompt, change to the folder where EventLogSourceInstaller.dll is located.
- Run the following command to create the EventSource:
InstallUtil EventLogSourceInstaller.dll
MORE INFORMATION
Steps to Reproduce the Behavior
- Use Visual Basic .NET or Visual C# .NET to create a new ASP.NET Web Application. By default, WebForm1.aspx file is created.
- In the HTML view of WebForm1.aspx, replace the existing code with the following sample code:
Visual Basic .NET Sample<%@ 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 Sample<%@ 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> - On the Debug menu, click Start to view the WebForm1.aspx page in the browser.
- Type some text in TextBox, and then click Write to event log.
- The error message that is discussed in the "Symptoms" section of this article appears.
- To resolve this problem, create an Event Source as discussed in the "Resolution" section, and comment the following code in WebForm1.aspx :EventLog.CreateEventSource(ev.Source, "Application")
- Repeat steps 3 and 4.
REFERENCES
For more information, visit the following Microsoft Web sites:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbwlkWalkthroughCreatingEventLogInstallers.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdiagnosticseventlogclasstopic.asp
| Last Reviewed: | 1/3/2003 |
| Keywords: | kbprb kberrmsg kbWebForms kbSecurity KB329291 kbAudDeveloper kbAudITPRO |
下一篇:动态生成柱状图 >>
相关文章:
- · 两个aspx页面间传递引用对象。
- · 在Webcontrol的Toolbar上加入删除确认的方法(改进后)
- · TreeView 派生类: TreeViewEx 实现 NodeShowToolTip、NodeDoubleClick 事件
- · 我自己写的自定义Web的上传控件
- · 增加判断文字长度,汉字算2个
- · 客户端脚本对中文的验证(javascript)
- · 献丑了,我的asp.net网站开发经验,欢迎参加讨论。
- · 笑望人生,关于IHttpHandler处理图片
- · HTML在线编辑器--服务器控件~~.NET实现~~
- · How to Share Session State Between Classic ASP and ASP.NET(1)
- · How to Share Session State Between Classic ASP and ASP.NET(2)
- · 关于验证控件,希望对和我原来有疑惑的朋友有帮助(刚找的资料,结合猫猫的)
- · 上次的一个问题我打了微软的求助电话,他们也没有办法!
- · [技巧]DataGird的hyper column的url field 绑定两个字段
- · ms--help
- · 续
- · Simple Paging in Repeater and DataList Controls
- · ASP.NET编程中的十大技巧(建议进精华)
- · 转贴:DataGrid/DataList
- · 用ASP.NET写你自己的代码生成器(1)。
- · ASP.NET中Cookie编程的基础知识(6)
- · ASP.NET中Cookie编程的基础知识(5)
- · ASP.NET中Cookie编程的基础知识(4)
- · ASP.NET中Cookie编程的基础知识(3)
- · ASP.NET中Cookie编程的基础知识(2)
- · ASP.NET中Cookie编程的基础知识(1)
- · .NET中窗体间相互访问的几种方式
- · .net中PictureBox中图片的拖动
- · 在.NET上如何根据字符串动态创建控件
- · .NET 窗体之间的交互
- · 使用UltraWinGrid时双击的处理
- · .Net 下的Wondows窗体常用项目
- · 在.net中实现与ASP完全兼容的MD5算法(包括中文字符)
- · .Net FrameWork SDK文档的例子演示
- · 利用.NET语言开发自己的脚本语言(一)
- · .NET中的数据类型的一些变化
- · 网上发现的文章(测试驱动开发)
- · .NET程序实现多语言
