如果用户通过验证后,我要带他到“家”的页面,但与包含用户配置文件的模型,否则,我要发布回相同的页面与模型的误差。下面是我做了什么:如何将模型传递给FormsAuthentication.RedirectFromLoginPage()?
[HttpPost]
[AllowAnonymous]
public ActionResult Index(Login loginViewModel)
{
if (ModelState.IsValid)
{
IUserProfile userProfile = ValidateUser(loginViewModel);
if (userProfile != null)
{
FormsAuthentication.RedirectFromLoginPage(loginViewModel.UserName, loginViewModel.RememberMe);
// return View(userProfile); This obviously won't work
}
else
{
ModelState.AddModelError("InvalidCredentials", "Invalid password. Please try again.");
}
}
return View();
}
的问题是,我如何通过视图模型(IUserProfile
)到Home/Index.cshtml
看法,因为我没有明确地重定向他的主页;它正在通过FormsAuthentication.RedirectFromLoginPage
方法完成。
我也不能拨打电话到FormsAuthentication.RedirectFromLoginPage
后返回View(userProfileViewModel)
,因为如果我这样做,它会,按照惯例,尝试Login
控制器上的名称Index
创建视图,并会尝试通过它的UserProfile
视图模型,这是错误的,并会导致异常。
一种方法是把USERPROFILE在会话对象,然后在HomeController.Index(),加载从会话配置文件,并把它传递给视图。 但是,如果你的建议,我需要通过模型到HTTPGET HomeController.Index(Models.UserProfile USERPROFILE),我该怎么办呢? –
@Water Cooler v2您可以通过调用User.Identity.Name来记录您的Index操作中的UserProfile。 – Gregoire