登录
前端js代码传用户名密码
控制器验证用户名密码是否正确
记住密码
system.web里面配置,否则无法用
public JsonResult GetAdminUser(AdminUser adminUser, bool opr)
{
Opretation opretation = new Opretation();
AdminUser user = adminUserService.Query(a=>a.Name==adminUser.Name&&a.Password==adminUser.Password);
opretation.Success = user != null;
if (opretation.Success)
{
//存session
AdminUserContext.context.UserInfo_no = user;
if (opr)//判断是否记住密码
{
string userData = user.Name + "&" + user.Id;
//存cookie
FormsAuthentication.SetAuthCookie(userData, opretation.Success);
}
}
return Json(opretation);
}
public ActionResult Login()
{
if (AdminUserContext.context.UserInfo_no != null)
{
return RedirectToAction("Index", "Home");
}
//再次浏览器判断是否有值
var name = this.User.Identity.IsAuthenticated ? this.User.Identity.Name : null;
if (name != null)
{
//获取存在的cookie
if (!string.IsNullOrEmpty(name))
{
//存储在session里面
AdminUser adminInfo = new AdminUser();
adminInfo.Id = Convert.ToInt32(name.Split('&')[1]);
adminInfo.Name = name.Split('&')[0];
AdminUserContext.context.UserInfo_no = adminInfo;
return RedirectToAction("Index", "Home");
}
}
return View();
}
首页欢迎
name即得到的用户名
@using MiShop.Admin.Models
@{
var name = this.User.Identity.IsAuthenticated ? this.User.Identity.Name : null;
}