会话为空

时间:2017-01-13 19:35:15

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

我的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;
    }

感谢。

1 个答案:

答案 0 :(得分:0)

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";
    }

}