Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从HttpModule调用Membership.GetUser()时失败_C#_.net_Asp.net - Fatal编程技术网

C# 从HttpModule调用Membership.GetUser()时失败

C# 从HttpModule调用Membership.GetUser()时失败,c#,.net,asp.net,C#,.net,Asp.net,我在ASP.NET应用程序的Global.asax文件中调用Membership.GetUser()方法,以便记录一些日志信息 但是,如果HttpModule内部发生错误,则它似乎会失败。这正常吗?在ASP.NET中执行HttpModules时,成员资格是否已准备就绪?我做错什么了吗 它抛出“未设置为对象实例的对象引用”异常(位于System.Web.Security.Membership.GetCurrentUserName(),位于System.Web.Security.Membership

我在ASP.NET应用程序的Global.asax文件中调用
Membership.GetUser()
方法,以便记录一些日志信息

但是,如果HttpModule内部发生错误,则它似乎会失败。这正常吗?在ASP.NET中执行HttpModules时,成员资格是否已准备就绪?我做错什么了吗


它抛出“未设置为对象实例的对象引用”异常(位于System.Web.Security.Membership.GetCurrentUserName(),位于System.Web.Security.Membership.GetUser())。

您可以从全局应用程序错误内部使用HttpApplication.User。 例如:

我用的是:

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
}

会话还不存在,成员资格存储其信息。FormsAuthentication.SetAuthCookie设置cookie,但该cookie已被读取

我将查看Global.asax.cs(或从HttpApplication派生的任何类)中的两个事件

  • 认证请求
  • 获得平等国家

你能说得更具体些吗?它是抛出错误还是仅仅返回null?是的,对不起。它抛出“未设置为对象实例的对象引用”异常(位于System.Web.Security.Membership.GetCurrentUserName(),位于System.Web.Security.Membership.GetUser()),HttpRequest在HttpHandler接管之前会遍历HttpModule堆栈。MembershipProvider就是这些模块之一。我假设您能够从webform或处理程序“获取用户”。另一方面,访问会员资格提供商可能是不可能的(我猜)。但是web.config中可能存在HtpModule配置问题。与此同时,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
}