//HTML代码
<script src="http://www.cnblogs.com/Repository/JavaScript/Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<table cellspacing="0" cellpadding="0" width="100%" border="0" height="143" id="table212">
<tr>
<td width="13%" height="38">
<span class="login_txt">用户名:</span>
</td>
<td height="38" colspan="2">
<input type="text" name="username" id="username" maxlength="15" class="textStyle" />
</td>
</tr>
<tr>
<td width="13%" height="35">
<span class="login_txt">密 码:</span>
</td>
<td height="35" colspan="2">
<input type="password" maxlength="15" id="password" name="password" class="textStyle" />
</td>
</tr>
<tr>
<td>
</td>
<td style=" 155px; font-weight: bold; color: Red; font-size: 12px">
<div id="msg">
</div>
</td>
<td align="left">
<input id="submitbtn" name="submitbtn" type="button" class="jhp" value=" 登 录 "
title="登录" />
<script type="text/javascript">
$(function () {
$("#submitbtn").click(function () {
var username = document.getElementById("username").value;
var userpwd = document.getElementById("password").value;
var msg = document.getElementById("msg");
msg.innerHTML = "";
if (!username) {
msg.innerHTML = "请输入用户名";
return false;
}
if (!userpwd) {
msg.innerHTML = "请输入密码";
return false;
}
var url = '../Login/LoginVar.aspx?loginid=' + username + '&loginpwd=' + userpwd;
$.get(url, { date: new Date().getTime() }, function (data) {
if (data) {
if (data.url) {
window.location = data.url;
}
else {
msg.innerHTML = data;
}
}
}, "json");
});
});
</script>
</td>
</tr>
</table>
// 后台Action代码
/// <summary>
/// 登陆验证
/// </summary>
/// <param name="fc"></param>
public ActionResult LoginVar(string username,string userpwd)
{
try
{
ZHUser user = userRepository.GetUser_username_password(username, Tools.GetMd5By16(userpwd));
if (user == null)
{
var json = "账号或密码不正确";
return Json(json, JsonRequestBehavior.AllowGet);
}
else
{
FormsAuthentication.SetAuthCookie(username, false);
HttpCookie userCookie = new HttpCookie("username", user.UserName);
HttpCookie userid = new HttpCookie("userid", user.Id.ToString());
Response.Cookies.Add(userid);
Response.Cookies.Add(userCookie);
var json = new { url = Url.Action("Index", "Main") };
return Json(json, JsonRequestBehavior.AllowGet);
//return RedirectToAction("Index", "Main");
}
}
catch (Exception ex)
{
var json = ex.Message;
return Json(json, JsonRequestBehavior.AllowGet);
}
}