您可以从Global的Application_Error中使用HttpApplication.User。 如:
User.Identity.Name
这里有一个我用:
protected void Application_Error(object sender, EventArgs e)
{
try
{
Exception lastError = Server.GetLastError().GetBaseException();
if (lastError is HttpException && ((HttpException)lastError).GetHttpCode() == 404)
return;
if (Request.UrlReferrer != null)
lastError.Data.Add("Referrer", Request.UrlReferrer);
if (Request.RawUrl != null)
lastError.Data.Add("Page", Request.RawUrl);
if (Request.UserHostAddress != null)
lastError.Data.Add("Client IP", Request.UserHostAddress);
if (Request.UserAgent != null)
lastError.Data.Add("UserAgent", Request.UserAgent);
if (User != null && User.Identity != null && !string.IsNullOrEmpty(User.Identity.Name))
lastError.Data.Add("User", User.Identity.Name);
Log.Error("Application_Error trapped at Global.asax", lastError);
}
// ReSharper disable EmptyGeneralCatchClause
catch { } // Intentionally empty catch clause as this is the catchall exception handler. If it fails, the sky has fallen.
// ReSharper restore EmptyGeneralCatchClause
}
你能更具体?它会抛出一个错误还是简单地返回null? – JoshJordan 2009-08-12 14:49:02
对,对不起。它抛出一个“对象引用未设置为对象的实例”。异常(在System.Web.Security.Membership.GetCurrentUserName(),在System.Web.Security.Membership.GetUser()) – 2009-08-12 14:53:16
HttpRequest在HttpHandler接管之前经过HttpModules堆栈。 MembershipProvider是这些模块之一。我假设你能够从webform或处理程序“GetUser”。另一方面可能无法获得MembershipProvider(我的猜测)。尽管可能是web.config中的HtpModule配置问题。同时User.Identity.Name似乎是要走的路。 – 2009-08-12 15:34:57