2015-12-01 32 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