public ActionResult Index()
{
User users = new User();
//判断当前cookie是否存在用户名和密码,若存在则传给前台,直接赋值
HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get("COOKIE_NAME_FOR_USER");
users.Name = (cookie == null ? "" : cookie["COOKIE_USER_NAME"].ToString().Trim());
users.Password = (cookie == null ? "" : cookie["COOKIE_USER_PASS"].ToString().Trim());
if (cookie != null)
{
ViewData["checked"] = "checked";
}
else
{
ViewData["checked"] = "";
}
return View(users);
}
public ActionResult Check()
{
string messge;
var remeber = Request.Form["Isre"];
string LoginName = Request.Form["name"];
string Password = Request.Form["password"];
var user = (from u1 in db.Users
where u1.UserName == LoginName && u1.Password == Password
select u1).FirstOrDefault();
if (remeber == "true")
{
//如果选中记住密码则把用户名和密码存入cookie否则存为空
HttpCookie cookie = new HttpCookie("COOKIE_NAME_FOR_USER");
cookie.Expires = DateTime.Now.AddYears(1);
cookie["COOKIE_USER_NAME"] = LoginName;
cookie["COOKIE_USER_PASS"] = Password;
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
}
else
{
HttpCookie cookie = new HttpCookie("COOKIE_NAME_FOR_USER");
cookie.Expires = DateTime.Now.AddYears(-1);
Request.Cookies.Add(cookie);
cookie["COOKIE_USER_NAME"] = null;
cookie["COOKIE_USER_PASS"] = null;
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
}
if (user == null)
{
messge = "error";
}
else
{
messge = "ok";
HttpContext.Session["Name"] = LoginName;
}
return Content(messge);
}
前台页面:
@model 沃德OA管理系统.Models.User
@{
Layout = null;
}
<script type="text/javascript">
function check() {
var uer = $("#ipt_loginname").val();
var pwd = $("#ipt_password").val();
var Isre = $("input[type='checkbox']").is(':checked')
if (uer == "") {
$("#error").html("用户名不能为空!!");
return;
}
if (pwd == "") {
$("#error").html("密码不能为空!!");
return;
}
$.ajax({
url: "/Login/Check",
type: 'POST',
dataType: 'text',
async:true,
data: { name: uer, password: pwd, Isre: Isre, },
error :function(strValue){
$("#error").html("请求失败!!");
},
success: function (strValue) {
if (strValue.trim() == "ok") {
window.location="/Home/Index";
}
else {
$("#error").html("用户名或密码错误!");
}
}
});
};
</script>
<style type="text/css">
</style>
<body>
<div style="background-image: url('../../Content/Image/u=0.jpg'); background-repeat: no-repeat; background-size: 100% 100%; color: white; height: 100%; left: 0; position: absolute; top: 0; width: 100%;">
<form method="post" id="loginform">
<div class="login" style="left: 50%; margin: 20% 0 20px -150px; position: relative;">
<p style="margin-top: 10px;">
<label for="ipt_loginname">登录账号</label>
<input id="ipt_loginname" name="LoginName" placeholder="请输入账号" type="text" value="@Model.Name"/>
</p>
<p>
<label for="ipt_password">登录密码</label>
<input id="ipt_password" name="Password" placeholder="请输入密码" type="password" value="@Model.Password" />
</p>
<div style="height:15px"></div>
<div style="text-align:right;margin-right:80px">
<input type="button" οnclick="check()" id="post" value="登录" />
<input id="Isre" name="Isre" type="checkbox" @ViewData["checked"] /><span style="font-size: 12px;">记住密码</span>
</div>
<p style="color: yellow; margin-left:100px" id="error"></p>
</div>
</form>
</div>
</body>