2015-12-01 36 views
0

我無法弄清楚什麼時候登錄到應用後會話第一次可以與用戶認證一起使用。 我的目標是在用戶登錄到應用程序後,將有關用戶的一些數據保存到會話中(在登錄之後,在發生其他任何事情之前),以便稍後使用它。 我在VS2015中使用默認的MVC 5模板。 我的登錄看起來像認證後的MVC 5會話

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); 
     switch (result) 
     { 
      case SignInStatus.Success: 
       return RedirectToLocal(returnUrl); //I jump here 
      case SignInStatus.LockedOut: 
       return View("Lockout"); 
      case SignInStatus.RequiresVerification: 
       return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); 
      case SignInStatus.Failure: 
      default: 
       ModelState.AddModelError("", "Invalid login attempt."); 
       return View(model); 
     } 

private ActionResult RedirectToLocal(string returnUrl) 
    { 
     //here I would like to save some stuff into the session, the session is available here but the user is not authenticated yet 

     if (Url.IsLocalUrl(returnUrl)) 
     { 
      return Redirect(returnUrl); 
     } 
     return RedirectToAction("Index", "Course"); 
    } 

確定,下一步我想的Global.asax:

protected void Application_AuthenticateRequest(object sender, EventArgs e) 
    { 
     //and of course here I have authenticated user but the session is null 
    } 

你能幫助我,好嗎?

+0

爲什麼你需要在會​​話中保存值? – Hadee

+0

那麼,我想保存一些用戶特定的數據,以便在應用程序中進一步使用。該會議看起來像一個很好的地方保存在那裏:-) –

回答

1

ASP將比以前更無狀態,這意味着最好不要使用像session這樣的基於狀態的對象。如果您使用的是MVC5,那麼您使用的是標識2.這意味着您的身份上下文中有ApplicationUser。所以你有幾乎所有的東西你需要使用ApplicationUser。如果您需要更多ApplicationUser,最好的方法是延伸ApplicationUser。在這種情況下,你根本不需要任何會話變量。以here爲例。

+0

非常好,謝謝你,你是對的。 我擴展了用戶,它的工作原理! –

+0

所以請評分! – Hadee