如何实现,按回车键,自动登录,在相应控件上添加onkeypress事件
function CheckCodePress(e){ var e = e||window.event if (e.keyCode ==13){//13在keyCode中代表回车键 //登录JS函数 Login(); } }
public ActionResult Index() { string cookieName=NewProductConsts.NewProCookieName;//常量 HttpCookie httpCookie = Request.Cookies[cookieName] if(httpCookie != null) { HttpContext.Session["userName"]=httpCookie.Values["userName"].ToString(); HttpContext.Session["password"]=httpCookie.Values["password"].ToString(); } ViewData["msg"]=string.Empty; return View(ViewData); }
登录页面,把用户的信息写到表单中,给表单起一个id,填好action,如<form id="curform action="../Login/Index" method="post">,提交的时候写一个函数,绑定到一个按钮上
function Login()
{
$("#curform").submit();
}
点击按钮之后,进入到action中
public ActionResult Index(string userName,string password,string checkCode) { string result=string.Empty; int id=-1; if(string.IsNullOrEmpty(userName)) { result="用户名不能为空!"; } else if(string.IsNullOrEmpty(password)) { result="密码不能为空!"; } else if(NewProductWS.GetInstance().Client.Login(NewProductConsts.header,userName,password,out id)) { string cookieName=NewProductConsts.NewProCookieName;//常量 HttpCookie cookie=new HttpCookie(cookieName); cookie.Values.Add("userName",userName); cookie.Values.Add("password",password); cookie.Expires=DataTime.Now.AddDays(9); Response.Cookie.Add(cookie); Session.Timeout=120; HttpContext.Session.Timeout=120; HttpContext.Session["userName"]=cookie.Values["userName"].ToString(); HttpContext.Session["password"]=cookie.Values["password"].ToString(); //登录成功,获取权限 NewProductWS.Cur_UserID=id; //保存用户名 NewProductWS.Cur_UserName=userName; HttpContext.Session["LoginTime"]=DataTime.Now.ToString(); //保存权限列表 BasePage.PermissionElementList=Newtonsoft.Json.JsonConvert.DeserializeObject<List<PermissionElementList>>(NewProductWS.GetInstance().client.GetUserPermission(NewProductConsts.header,id)); HttpSession.Session["Permissions"]=BasePage.PermissionElementList; return RedirectToAction("Index","Home");//action-->action } else { result="用户名或密码错误!"' } ViewData["msg"]=result; return View(ViewData); }