【问题标题】:Session is null会话为空
【发布时间】:2017-01-14 15:29:32
【问题描述】:

我的 global.asax 中的一个事件调用了类中的一个方法,该方法返回一个整数。我想将此整数保存在 global.asax 中的会话变量中,稍后我想在控制器中使用它。我尝试了 HttpContext.Current.Session.Add,但它给出了“对象引用未设置为对象实例”。我不知道如何解决这个问题,非常感谢任何帮助。

这里是示例代码

public void WSFederationAuthenticationModule_SecurityTokenValidated(object sender, SecurityTokenValidatedEventArgs e)
    {
        int ID = new StudentClass.GetStudentID(e.ClaimsPrincipal);
        //Im trying to store this ID in a session
        var ru = GetReturnUrl(Request);

        if (!string.IsNullOrEmpty(ru))
        {
            HttpContext.Current.Response.Redirect(ru);
        }

        e.Cancel = true;
    }

谢谢。

【问题讨论】:

  • 嗨,你在哪个方法中设置这个方法?
  • 请给我看看 global.asax 文件的代码。你应该把它 session_start 方法请尝试一下
  • 在全局 asax 文件中的 WSFederationAuthenticationModule securitytokenvalidated 事件下。
  • 请粘贴此文件的完整代码,这会很有帮助。还告诉我项目类型是什么? Asp.net forms/mvc ?
  • 我在上面编辑了我的问题。我正在使用 asp.net mvc

标签: asp.net asp.net-mvc session


【解决方案1】:
Try this was as mention by me in my comment . You can access ClaimPrincipal object in app_start / session_start event and then stored it .I hope it helps 

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup


    }
     void Session_Start(object sender, EventArgs e)
    {
       -- here you can get the StudentID and then store it.
        var t = ClaimsPrincipal.Current;
        Session["StudentId"] = "Yash";
    }

}

【讨论】:

    猜你喜欢
    • 2017-07-24
    • 2019-08-29
    • 2020-11-09
    • 2016-10-15
    • 1970-01-01
    • 2012-05-24
    • 2013-04-30
    • 2012-07-07
    相关资源
    最近更新 更多