嗨,这是我的登录控制器,用于登录页面。我已经完成了所有的检查和登录工作,但是我没有创建登录会话,因此我的网站不知道使用数据库中的ID和密码登录的用户是谁。所以即使进行了登录,网站也只能识别一个用户。
如果我要使用按钮进行选择,我还需要知道如何检索登录会话。例如:“用户Z在使用用户名和密码登录后选择了 WorkSchedule
A”
如果没有为网站保存会话,登录将不完整。我在保存会话方面遇到了问题,希望有人能指导我如何解决。
控制器代码:
[HttpGet]
public ActionResult Login()
{
return View();
}
void connectionString()
{
con.ConnectionString = " ";
}
[HttpPost]
public ActionResult SaveData(Account acc)
{
connectionString();
con.Open();
com.Connection = con;
com.CommandText = "insert into Staff (StaffNRIC,StaffEmail,StaffContact,StaffName,StaffAddress,BranchID,StaffRole,StaffPositionID,StaffAccountStatus)" +
"values ('" + acc.StaffNRIC + "','" + acc.StaffEmail + "','" + acc.StaffContact + "','" + acc.StaffName + "','" + acc.StaffAddress + "','" + acc.BranchID + "',' NULL ','" + acc.StaffPositionID + "', 'Pending' )";
dr = com.ExecuteReader();
if (dr.Read())
{
con.Close();
return View("Register");
}
else
{
con.Close();
return View("Login");
}
}
[HttpPost]
public ActionResult Verify(Account acc)
{
connectionString();
con.Open();
com.Connection = con;
com.CommandText = "select * from Staff where StaffNRIC='" + acc.StaffNRIC + "' and StaffContact='" + acc.StaffContact + "' and StaffAccountStatus = 'Approved'";
dr = com.ExecuteReader();
if (dr.Read())
{
con.Close();
return View("Home");
}
else
{
con.Close();
return View("Login");
}
}
查看页面:
<form action="Verify" method="post">
<div class=" w3l-form-group">
<label>NRIC:</label>
<div class="group">
<i class="fas fa-user"></i>
<input type="text" name="StaffNRIC" class="form-control" placeholder="StaffNRIC" required="required">
</div>
</div>
<div class=" w3l-form-group">
<label>Password:</label>
<div class="group">
<i class="fas fa-unlock"></i>
<input type="password" name="StaffContact" class="form-control" placeholder="StaffContact" required="required">
</div>
</div>
<button type="submit">Login</button>
</form>
</div>