我有一个非常基本的 asp.net 应用程序,它依赖于母版页的 INIT 事件来通过会话对象验证用户。是的,我知道这是次优的。
我想向其中添加 ELMAH,但在不使用表单身份验证和 web.config 允许/拒绝设置的情况下找不到任何保护控制台的参考。
是否有另一种方法来保护不依赖于表单身份验证的 elmah.axd 文件?
本文介绍如何将 Elmah 错误处理程序包装在另一个允许访问会话状态的事件处理程序中:
http://groups.google.com/group/elmah/msg/a0aa776d348ba97e
在 Global.asax 中,您可以拥有如下内容:
protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
// Get the filename being requested
string filename = Path.GetFileName(Request.Path).ToLower();
// Verify that a user is logged in by checking the session
bool isValid = (HttpContext.Current.Session["User"] != null);
// Throw error if invalid
if (filename == "elmah.axd" && !isValid)
throw new SecurityException("Access to elmah.axd is denied");
}
标准 Elmah 处理程序没有实现 IRequiresSessionState 或 IReadOnlySessionState,因此您必须创建另一个事件处理程序来包装它,如上面提到的链接中所述。否则,您将无法访问 Application_PreRequestHandlerExecute 事件中的会话。
我是 ELMAH 的 MySQL 支持开发人员之一。允许/拒绝选项不是特定于 Web 表单的。他们可以与任何提供商合作。
在 global.asax 或您自己的 IHttpHandler 中的 Request_Authentication 上,您需要创建 IPrincipal 对象并将其设置为 Context.User 对象。像这样的东西:
private void Request_Authenticate (object sender, EventArgs e) {
// do checks and create IPrincipal object
IPrincipal user = new MyPrincipal(...);
Context.User = user;
}
然后,当为您的 elmah.axd 处理程序检查角色时,它将针对此自定义对象而不是 Web 表单。
开箱即用,远程访问被禁用,因此您只能从本地计算机访问错误日志。这里还有更多: http ://code.google.com/p/elmah/wiki/SecuringErrorLogPages