前台
@if (Session["username"] != null){<div class="login"><span style="width:155px;height:85px;display:inline-block;margin-left:50px;margin-top:25px;text-align:center">@(Session["username"]) 您好!<br />欢迎登录@(ChaoXingPMS.Utils.Site.Name)。<br /><a href="interfaceapi?mode=exit&backurl=@(Server.UrlEncode(Request.Url.AbsoluteUri))">退出</a></span></div>}else{<div class="login">证 号: <input type="text" name="username" id="username" class="zjh" /><br />密 码: <input type="password" name="pw" id="pw" class="mm" /><br /><input class="login_btn" type="button" value="登录" name="button" id="login" style="cursor:pointer"/> </div>}
js:
$(function () {$("#btn_login").click(function () {$(".pop_login").css("display", "block");$(".pop_last").css("display", "block");});$("#close").click(function () {$(".pop_login").css("display", "none");$(".pop_last").css("display", "none");});$("#username").keydown(function () {if (event.keyCode == 13) //回车键的键值为13 $("#login").click(); //调用登录按钮的登录事件 })$("#pw").keydown(function () {if (event.keyCode == 13) //回车键的键值为13 $("#login").click(); //调用登录按钮的登录事件 })$("#login").click(function () {var name = $("#username").val();var pw = $("#pw").val();if (name == "" || pw == "") {$("#tip").html("<em>* </em>账号密码不能为空");return;}$.ajax({type: "post",dataType: "json",data: "username=" + name + "&password=" + pw + "&mode=" + loginmode + "&time=" + (new Date().getTime()),//url: "InterFaceAPI",url: "/interface/LoginData.cshtml",error: function (XmlHttpRequest, textStatus, errorThrown) { alert(XmlHttpRequest.responseText); },success: function (d) {if (d.status == 1) {$("#tip").html("");//window.location = '/'; location.reload();//$("#logindiv").html("<div class='login_top'>用户登录</div><span style='line-height:58px;'>" + d.uname + " 您好!<br/>欢迎登录" + d.wname + "。<a href='/interface/LoginData.cshtml?outtype=logout'> 退出</a></span>"); }else {$("#tip").html("<em>* </em>" + d.message);}}});}); })
ajax请求:
var username = HttpContext.Current.Request["username"];var password = HttpContext.Current.Request["password"];var loging_url = appSetting["loging_url"].ToString(); var pwdm5 = ChaoXingPMS.Utils.MD5.Lower32(password);string formUrl = loging_url;//url地址string returnUrl = "";//string logintypej = "CALLNO";//string URL = "ReaderTable.aspx";string formData = "returnUrl=" + returnUrl + "&rdid=" + username + "&rdPasswd=" + pwdm5;CookieContainer cookieContainer = new CookieContainer();// 将提交的字符串数据转换成字节数组 byte[] postData = Encoding.UTF8.GetBytes(formData);HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;Encoding myEncoding = Encoding.GetEncoding("gb2312");request.Method = "POST";request.KeepAlive = false;request.AllowAutoRedirect = true;request.ContentType = "application/x-www-form-urlencoded";request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";request.CookieContainer = cookieContainer;request.ContentLength = postData.Length;// 提交请求数据 System.IO.Stream outputStream = request.GetRequestStream();outputStream.Write(postData, 0, postData.Length);outputStream.Close();HttpWebResponse response;Stream responseStream;StreamReader reader;string srcString;response = request.GetResponse() as HttpWebResponse;responseStream = response.GetResponseStream();reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);srcString = reader.ReadToEnd();reader.Close();//关闭流对象 reader.Close();responseStream.Close();reader.Close();response.Close();//确认是否能获取权限 if (srcString.IndexOf("欢迎您") > 0){Session["isNowLogin"] = 1;//是否是登陆动作Session["isLogin"] = 1;//是否登陆成功Session["username"] = username;Session["password"] = password;Session["logingMsg"] = "登录成功!";if (IsAjax){ExecuteResult(c => c.Json(new { status = 1, uname = username, upwd = password}));return;}}else{Session["isNowLogin"] = 1;Session["isLogin"] = 0;Session["logingMsg"] = "登录失败!";string message = "登录失败!";if (IsAjax){ExecuteResult(c => c.Json(new { status = 0, message = message }));return;}}