MVC前台代码
-
@{
-
ViewBag.Title = "Index";
-
}
-
<script src="~/Scripts/jquery-1.10.2.min.js"> </script>
-
<script type="text/javascript">
-
function userLogin() {
-
var url = '@Url.Action("UserLogin","Home")';
-
var UserName = $( '#UserName').val();
-
var Password = $( '#Password').val();
-
var DoRemember = $( '#DoRemember').is( ':checked');
-
$.post(url, { UserName: UserName, Password: Password, DoRemember: DoRemember }, function (result) {
-
if (result.toUpperCase() == 'TRUE') {
-
alert( '登录成功!');
-
}
-
});
-
}
-
</script>
-
<h2>Index </h2>
-
<table>
-
<tr>
-
<td>用户名 </td>
-
<td> <input type="text" id="UserName" value="@Model.UserName" /> </td>
-
</tr>
-
<tr>
-
<td>密码 </td>
-
<td> <input type="password" id="Password" value="@Model.Password" /> </td>
-
</tr>
-
<tr>
-
<td>
-
<input id="DoRemember" type="checkbox" />记住密码
-
</td>
-
<td> </td>
-
</tr>
-
<tr>
-
<td> <input type="button" value="登录" onclick="userLogin();" /> </td>
-
<td> </td>
-
</tr>
-
</table>
MVC后台代码
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Web.Mvc;
-
-
namespace WebApplication100.Controllers
-
{
-
public class HomeController : Controller
-
{
-
public ActionResult Index()
-
{
-
HttpCookie cookie = Request.Cookies[ "UserInfoRemember"];
-
Student Model = new Student();
-
if (cookie != null)
-
{
-
Model.UserName = cookie[ "UserName"].ToString();
-
Model.Password = cookie[ "Password"].ToString();
-
}
-
return View(Model);
-
}
-
/// <summary>
-
///登录
-
/// </summary>
-
/// <param name="UserName">用户名</param>
-
/// <param name="Passwrod">密码</param>
-
/// <param name="Remeber">是否记住用户名、密码</param>
-
[]
-
public bool UserLogin(string UserName, string Password, bool DoRemember)
-
{
-
if (DoRemember)
-
{
-
HttpCookie cookie = new HttpCookie( "UserInfoRemember");
-
cookie.HttpOnly = true;
-
cookie[ "UserName"] = UserName;
-
cookie[ "Password"] = Password;
-
cookie.Expires = DateTime.MaxValue;
-
Response.Cookies.Add(cookie);
-
}
-
else
-
{
-
HttpCookie cookie = Request.Cookies[ "UserInfoRemember"];
-
if (cookie != null)
-
{
-
cookie.Expires = DateTime.Now.AddDays( -1); //立即过期
-
Response.Cookies.Add(cookie); //重新写入才能使Cookies["userinfo"]失效*/
-
}
-
}
-
return true;
-
}
-
}
-
public class Student
-
{
-
public string UserName { get; set; }
-
public string Password { get; set; }
-
}
-
}