In my project I made a filter to see if the user is already logged in. To prevent it from logging in twice.
According to the answer
What happens is that I am having a zero reference error and I do not know why. I am passing the ID of the user in the session, but I am not able to capture it at the time of doing this check, generating me this error:
Description: An unhandled exception occurred during the execution of the current Web request. Examine the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
And the error is being generated on this line:
var idDoUsuario = (long) filterContext.HttpContext.Session["UsuarioID"];
At the level of more information, I'll put the class codes I use for the filter and the action that I use to authenticate.
Filter
void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
{
var contexto = new EntidadesContexto();
// Recupero a Id do Usuário logado aqui.
// Não sei como você está fazendo, então inventei um método
var idDoUsuario = (long) filterContext.HttpContext.Session["UsuarioID"];
var usuario = contexto.Usuarios.FirstOrDefault(u => u.UsuarioID == idDoUsuario && u.Logado && u.SessionID != filterContext.HttpContext.Session.SessionID);
if (usuario != null)
{
// Se o último login foi feito dentro do período de um dia
if (usuario.UltimoLogin.AddDays(1) > DateTime.Now)
{
// Usuário logado em outro lugar.
usuario.Logado = false;
contexto.Entry(usuario).State = EntityState.Modified;
contexto.SaveChanges();
// Destrua aqui a Session do Usuário se houver uma.
}
else
{
filterContext.HttpContext.Session.Abandon();
// O login do Usuário expirou.
var controller = (MeuControllerBase)filterContext.Controller;
filterContext.Result = controller.RedirectToAction("Index", "Autenticacao");
}
}
Authentication
[FiltroSessao]
[HttpPost]
public ActionResult Index(String Login, String Senha)
{
//verificando login pelo usuario do banco de dados ...
Usuario login = db.Usuarios.Where(x => x.Login == Login && x.Senha == Senha).FirstOrDefault();
if (login != null)
{
FormsAuthentication.SetAuthCookie(login.Nome.ToString(), false);
Session.Add(".PermissionCookie", login.Perfil);
Session.Add("UsuarioID", login.UsuarioID);
login.Logado = true;
login.UltimoLogin = DateTime.Now;
login.SessionID = HttpContext.Session.SessionID;
db.Entry(login).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index", "Home"); //pagina padrao para todos os usuarios...
}
return RedirectToAction("Index");
}
Just to highlight, I pass the ID of the user in this line:
Session.Add("UsuarioID", login.UsuarioID);
Any suggestions?