2017-01-13 98 views
0

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

感謝。

+0

嗨在哪種方法你設置這種方法? –

+0

請向我展示global.asax文件的代碼。你應該把它放到session_start方法中,請嘗試 –

+0

在全局asax文件中的WSFederationAuthenticationModule securitytokenvalidated事件下。 – USK

回答

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

}