我喜欢在mvc 4 razor sql中将id
(主键)设置为session而不是password
。谢谢大家!
我的模特
public bool check(string Pass)
{
bool convert = false;
string configuration = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(configuration))
{
con.Open();
var sql1 = "select ID(Primary key) from employees where Pass = '" + Pass + "' ";
MySqlCommand cmd = new MySqlCommand(sql1, con);
convert = Convert.ToBoolean(cmd.ExecuteScalar());
con.Close();
return convert;
}
}
这是我的控制器
if (ModelState.IsValid)
{
if (person.check(person.Pass))
{
Session["Pass"] = person.Pass;
Session.Timeout = 100000;
return Content("<script language='javascript' type='text/javascript'>alert('Welcome User'); window.location.href='Home/Mainmenu';</script>");
}
else
{
return Content("<script language='javascript' type='text/javascript'>alert('Invalid UserID'); window.location.href='Home/Login';</script>");
}
}
答案 0 :(得分:0)
Session["Pass"] = person.Pass;
将其更改为
Session["Pass"] = person.yourPKeyID;