Recently, the project was developed using MVC, And the login problem was first encountered. It was a simple feature to be developed using aspx before. using MVC, the problem was easily solved and recorded, after thinking for a long time, I decided to use Ajax for verification. After the verification is passed, I directly submitted the jump. jquery verification is as follows. I won't talk about it if I need to reference jquery.
Function loginvalidate () {var KK = true; $. ajax ({type: "Post", datatype: "text", URL: "/helpers/admin_ajax.ashx", cache: false, timeout: 10000, async: false, data: {sign: "checklogin", Username: $ ("# username "). val (), password: $ ("# password "). val (), validatecode: $ ("# validatecode "). val ()}, success: function (data) {If (Data! = "True") {$ ("# lbltip "). show (); $ ("# lbltip" ).html (data); KK = false ;}}, error: function (data) {$ ("# lbltip" Login .html ("Logon Failed, Please refresh the page and try again"); KK = false ;}); return KK ;}
The login. cshtml page has a place where the onsubmit = "Return loginvalidate ()" or onclick = "Return loginvalidate ()" must be specified in the main form to ensure that the submission is terminated.
Admin_ajax.ashx,
/// <Summary> /// logon verification /// </Summary> /// <Param name = "context"> </param> /// <returns> true verify </returns> private string loginvalidate (httpcontext context) {string username = sysrequest. getformstring ("username"); string Password = sysrequest. getformstring ("password"); string validatecode = sysrequest. getformstring ("validatecode"); If (context. session [keys. session_code] = NULL) {return "The Verification Code does not exist. Please switch to the Verification Code Verification code! ";} If (string. isnullorempty (username) {return "enter user name";} If (string. isnullorempty (password) {return "Enter Password";} If (string. isnullorempty (validatecode) {return "Enter the verification code";} If (validatecode. tolower ()! = Context. session [keys. session_code]. tostring (). tolower () {return "the verification code is incorrect. Enter";} dictionary <string, Object> P = new dictionary <string, Object> (); P ["loginname"] = username; P ["password"] = utils. encrypt (password); p_manager item = sqldbhelper. getModel <p_manager> (p, sqldbhelper. constr); If (item = NULL) {return "the user name or password is incorrect. Enter";} context. session [keys. session_admin_info] = item; // write the logon LOG model. siteconfig = configcrud. loadconfig <siteconfig> (utils. getxmlmappath (keys. file_site_xml_confing); If (siteconfig. logstatus = 1) {p_manager_log logitem = new p_manager_log (); logitem. user_id = item. ID; logitem. user_name = item. loginname; logitem. action_type = "login"; logitem. note = "User Logon"; logitem. login_ip = sysrequest. getip (); logitem. login_time = datetime. now; sqldbhelper. insertmodel <p_manager_log> (logitem, sqldbhelper. constr);} If (item. roletype> 1) // super management does not judge the permission {string SQL = "select [modulename], [moduleid], [functiondetails] from [p_functionmodule_role] Where roleid =" + item. roleid; system. data. datatable dt = sqldbhelper. sqltextdataset (system. data. commandtype. text, SQL, sqldbhelper. constr ). tables [0]; If (DT. rows. count> 0) {string STR = ""; for (INT I = 0; I <DT. rows. count; I ++) {if (I = DT. rows. count-1) {STR + = DT. rows [I] ["modulename"]. tostring () + ":" + dt. rows [I] ["moduleid"]. tostring () + ":" + dt. rows [I] ["functiondetails"]. tostring ();} else {STR + = DT. rows [I] ["modulename"]. tostring () + ":" + dt. rows [I] ["moduleid"]. tostring () + ":" + dt. rows [I] ["functiondetails"]. tostring () + "," ;}} context. session ["adminrole"] = STR ;}return "true ";}
The logincontroller code after submission is as follows:
[HttpPost] public ActionResult Login(FormCollection form) { string cbRememberId = Request.Form["cbRememberId"]; if (cbRememberId == "on") { Utils.WriteCookie("DTRememberName", form["username"], 14400); } else { Utils.WriteCookie("DTRememberName", form["username"], -14400); } return RedirectToAction("Index", "Home"); }
Here, a checkbox is used to save the user name, and then submitted to the Controller of the home page,