我正在开发一个ASP.NET MVC4应用程序,我需要在登录2存储过程之后调用以获取用户特定的详细信息...我有一个Login控制器来执行此操作
[HttpPost]
public ActionResult Login(UserLogin user)
{
if (ModelState.IsValid)
{
bool res = System.Web.Security.Membership.ValidateUser(user.UserName, user.Password);
if (res)
{
Utente utente = commonRepository.GetProfiloUtente(user.UserName);
if (utente != null)
{
Session["utente"] = utente;
}
DateTime dataLavorativa = commonRepository.GetGiornoLavorativoPrecedente(utente.IDInterno);
Session["data_lavorativa"] = dataLavorativa;
FormsAuthentication.SetAuthCookie(user.UserName, user.RememberMe);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", "Login data is incorrect!");
}
}
return View("Index", user);
}
当用户未经过身份验证且被迫从登录页面传递时,此功能正常运行...
当用户连接到应用但他已经过身份验证时,我应该在哪里放置代码以调用这些方法?
我不能把它放在每个控制器的每个Index动作中...... 感谢
答案 0 :(得分:1)
我认为您可以使用Global.asax - Application_PreRequestHandlerExecute方法。
public void Application_PreRequestHandlerExecute(Object source, EventArgs e)
{
bool isAuthenticated = (System.Web.HttpContext.Current.User != null)
&& System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
if (isAuthenticated )
{
//Do call procedures
}
}
希望它有所帮助。
答案 1 :(得分:0)
我已经修复了那个部分......我在App_Start中添加了eventhandler而不是控制器......因为我处于集成模式,我无法访问PreRequestHandlerExecute中的会话对象,我选择了创建一个BaseController并覆盖它的OnAuthorization ...这是错的吗?在这个方法里面,我读了它是否已经过身份验证,如果我已经有一个Session [“user”] ...如果不是我从cookie中取出userID并调用2个存储过程来检索数据......这可以吗? 感谢