嗨,我正在使用 c# 在 mvc4 中做我的 web 项目。现在我正在创建一个登录页面。我使用的以下代码。用户名和密码在 sql 数据库表中
看法
@using (Html.BeginForm())
{
@Html.LabelFor(model => model.Mem_Email)
@Html.EditorFor(model => model.Mem_Email)
@Html.ValidationMessageFor(model => model.Mem_Email)
@Html.LabelFor(model => model.Mem_PWD)
@Html.EditorFor(model => model.Mem_PWD)
@Html.ValidationMessageFor(model => model.Mem_PWD)
<input type="submit" value="Log In" />
}
控制器
public ViewResult Login()
{
return View();
}
[HttpPost]
public RedirectResult Login(FormCollection form)
{
string uid = Request.Form["Log_email"];
string pwd = Request.Form["Log_pwd"];
bool IsUser=new Member().GetLogin(uid,pwd);
if (IsUser == true)
{
System.Web.Security.FormsAuthentication.SetAuthCookie(uid, true);
return Redirect("~/Member/MemberHome");
}
else
return Redirect("~/Member/Login");
}
模型
public bool GetLogin(string email,string pwd)
{
bool IsUser = false;
using (SqlConnection con = new SqlConnection(Config.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(
"SELECT COUNT (*) FROM Mem_Register WHERE Mem_Email='" +
email + "' AND Mem_PWD='" + pwd + "'", con))
{
con.Open();
int count = (int)cmd.ExecuteScalar();
if (count == 1)
{ IsUser = true; }
}
}
return IsUser;
}
这不起作用。表单中的内容不会传递给控制器。我不知道这是登录用户的正确方法。请帮我。