我有一个登录页面和一个具有“登录”操作的帐户控制器。当我登录时,我被重定向到主页(很好),但是登录后如果我重新访问登录页面,它将再次显示登录表单(尽管我已经登录)。 我尝试检查会话状态值,但是每次尝试使用它时,都会得到空引用错误。
public ActionResult Login(string name, string password, string hash)
{
if (!string.IsNullOrWhiteSpace(name))
{
var user = _model.tblUsers.FirstOrDefault(x => x.username == name);
if (user != null)
{
if (user.powerLevel == 0)
{
Session["IsAdmin"] = (user.password == password);
Session["IsAuthor"] = null;
Session["IsUser"] = null;
}
else if (user.powerLevel == 1)
{
Session["IsAdmin"] = null;
Session["IsAuthor"] = (user.password == password);
Session["IsUser"] = null;
}
else if (user.powerLevel == 2)
{
Session["IsAdmin"] = null;
Session["IsAuthor"] = null;
Session["IsUser"] = (user.password == password);
}
else
{
return View("Login");
}
return RedirectToAction("Index","Posts");
}
}
return View("Login");
}
因此,如果IsAdmin,IsAuthor,IsUser Session中的任何一个设置为true,我都希望重定向到主页。我尝试用string.IsNullOrWhiteSpace进行检查,但是它不起作用,即使Session设置为true,我也总是会报错