业务系统实现记住密码和自动登录功能
業(yè)務系統(tǒng)實現(xiàn)記住密碼和自動登錄功能
公司的業(yè)務系統(tǒng)本來是受域控的,用戶不需要登錄可以直接訪問系統(tǒng)。當然,雖然不用人工登錄,系統(tǒng)本身會讀取電腦的用戶名為登錄標識,對系統(tǒng)操作權限和記錄也是以電腦名。近段時間,由于系統(tǒng)要牽到云端,也就是不受域控了,那就需要每人手頭上都有賬號和密碼了,這個和一般的業(yè)務系統(tǒng)沒什么區(qū)別。但是由于用戶之前的習慣是不用登錄的,而且每天打開關閉的次數較多。OK,一般的系統(tǒng)登錄都會有個記住密碼的功能,但是,這還滿足不了用戶的需求,那么我們給用戶增加多一個自動登錄功能,類似QQ那樣,我上次訪問勾選了自動登錄功能,然后再次訪問就不用再去登錄了。
先來看看原來讀取域用戶的代碼吧。
string strname = HttpContext.Current.User.Identity.Name;//獲取到帶域名的用戶string name = HttpContext.Current.User.Identity.Name.Substring(HttpContext.Current.User.Identity.Name.IndexOf("\\") + 1, HttpContext.Current.User.Identity.Name.Length - HttpContext.Current.User.Identity.Name.IndexOf("\\") - 1);很多童鞋這樣子獲取到的是空的,是因為web.config沒設置好。必須在配置文件<authentication mode="Windows">把mode的屬性設為Windows,具體位置在<configuration><system.web>里面。
好吧,下面就來實現(xiàn)我們的記住密碼和自動登錄功能吧。給大家看看登錄界面吧。
先跟大家說說具體的功能吧
1、當用戶勾選記住密碼時,下次打開登錄頁面自動填充用戶名和密碼,用戶只需點擊登錄按鈕即可以登錄,當然,你也可以刪掉自動填充的密碼自己重新輸入,或者你用另外的用戶密碼登錄也行。
2、當用戶勾選自動登錄時,記住密碼也會自動被勾選上,因為只有記住了密碼才會有自動登錄,這是合理存在的。下次用戶打開登錄頁面時,將會自動驗證跳轉到驗證通過后的頁面,不再需要用戶去點擊登錄按鈕。
?先看看前端的簡要代碼,簡單點把需要用到的控件和js貼出來就好了,其它布局的就不貼出來了。js簡單解釋一下
<form runat="server"> <input type="hidden" id="hidPass" runat="server" /> <input type="text" runat="server" id="txtLoginName" /> <asp:TextBox runat="server" ID="txtPassWord" class="textwidthheigh" TextMode="Password"></asp:TextBox> <input type="checkbox" runat="server" value="記住密碼" id="chkRemember" onclick="CheckRemember()" /> <input type="checkbox" runat="server" value="自動登錄" id="chkLogin" onclick="CheckLogin()" /> <asp:Button runat="server" id="btnLogin" onclick="btnLogin_Click" /> <input type="button" id="btnClear" onclick="Clear()" /> </form><script type="text/javascript" language="javascript"> //頁面加載用戶名輸入框獲得焦點document.getElementById("txtLoginName").focus();function Clear() { //用戶點擊取消,清空用戶名和用戶密碼
document.getElementById("txtLoginName").value = "";document.getElementById("txtPassWord").value = "";}function CheckLogin() {//用戶勾選自動登錄時,把記住密碼也勾選上
var remember = document.getElementById("chkRemember");remember.checked = true;}function CheckRemember() {var remenber = document.getElementById("chkRemember");var login = document.getElementById("chkLogin");if (remenber.checked == false) {login.checked = false;//用戶去掉記住密碼時,也把自動登錄去掉
}}</script>
?
protected void Page_Load(object sender, EventArgs e){
if (!IsPostBack){
//獲取客戶端的Cookies,分別兩個cookies,一個登陸名,一個密碼
HttpCookie LoginNameCookie = Request.Cookies["Bic_LoginName"];HttpCookie LoginPassCookie = Request.Cookies["Bic_Pass"];if (LoginNameCookie != null){//登錄名的cookies不為空,填充登陸名
txtLoginName.Value = LoginNameCookie.Value;}if (LoginPassCookie != null){//密碼cookies不為空,給密碼框和隱藏密碼框填充,當然我們的密碼是加密過才存到cookies去的,至于以藏文本框的作用后面就會看到
this.txtPassWord.Attributes.Add("value",LoginPassCookie.Value+"");hidPass.Value = LoginPassCookie.Value + "";//賦值給隱藏控件chkRemember.Checked = true;}//獲取是否有勾選自動登錄的cookies
HttpCookie Login = Request.Cookies["Bic_LoginAuto"];//當用戶在系統(tǒng)點擊退出時
if (Request["opFlag"] == "Exit"){this.txtPassWord.Attributes.Add("value", "");//把密碼去掉chkRemember.Checked = false;//記住密碼去掉HttpCookie loginNameCookie = Request.Cookies["Bic_LoginName"];//獲取登錄名cookies
HttpCookie loginPassCookie = Request.Cookies["Bic_Pass"];//獲取密碼cookies
if (loginNameCookie != null){//把cookies時間設為-2相當于刪掉了cookies
loginNameCookie.Expires = DateTime.Now.AddDays(-2);Response.Cookies.Set(loginNameCookie);}if (loginPassCookie != null){//把密碼的cookies也刪掉
loginPassCookie.Expires = DateTime.Now.AddDays(-2);Response.Cookies.Set(loginPassCookie);}//自動登錄cookies也一樣
HttpCookie login = Request.Cookies["Bic_LoginAuto"];if (login != null){login.Expires = DateTime.Now.AddDays(-2);Response.Cookies.Set(login);}}else//用戶打開登錄界面時
{//自動登錄cookies不為空,用戶名不為空,隱藏框密碼不為空
if (Login != null && txtLoginName.Value != "" && hidPass.Value != ""){SysUser user = new SysUser();user.Login_Name = txtLoginName.Value;user.Login_Pass = hidPass.Value;int i = SysUserBLL.Login(user);//驗證登錄
if (i > 0){//成功登錄跳轉到default.aspx頁面
Page.Session["Login_Name"] = user.Login_Name;HttpContext.Current.Session["Display_Name"] = SysUserBLL.getDisplayNameByname(user.Login_Name);Response.Write("<script>window.location='Default.aspx';</script>");Response.End();}}}}
}
//點擊登錄按鈕事件
protected void btnLogin_Click(object sender, EventArgs e)
??????? {
//判斷是否為空
??????????? if (txtLoginName.Value.Trim() != "" && txtPassWord.Text.Trim() != "")
??????????? {
??????????????? SysUser user = new SysUser();
??????????????? user.Login_Name = txtLoginName.Value.Trim();
??????????????? user.Login_Pass = CommonHelper.MD5encipher(txtPassWord.Text.Trim());//MD5加密
??????????????? HttpCookie LoginNameCookie = Request.Cookies["Bic_LoginName"];
??????????????? HttpCookie LoginPassCookie = Request.Cookies["Bic_Pass"];
??????????????? if (LoginNameCookie != null)//如果是記住密碼情況
??????????????? {
??????????????????? if (txtLoginName.Value.Trim() == LoginNameCookie.Value.Trim())//讀取到cookies保存的用戶名和文本框用戶名相同,預防用戶又改動
??????????????????? {
??????????????????????? if (LoginPassCookie != null)
??????????????????????? {
??????????????????????????? if (txtPassWord.Text.Trim() == LoginPassCookie.Value.Trim())//cookies讀取到的密碼和文本框密碼相同
??????????????????????????? {
??????????????????????????????? user.Login_Pass = txtPassWord.Text.Trim();
??????????????????????????? }
???????????????????????????
??????????????????????? }
??????????????????? }
??????????????? }
??????????????? int i = SysUserBLL.Login(user);//驗證登錄
??????????????? if (i > 0)
??????????????? {
??????????????????? if (chkRemember.Checked == true)//記住密碼
??????????????????? {
??????????????????????? HttpCookie loginNameCookie = new HttpCookie("Bic_LoginName", user.Login_Name);
??????????????????????? HttpCookie loginPassCookie = new HttpCookie("Bic_Pass", user.Login_Pass);
??????????????????????? loginPassCookie.Expires = DateTime.Now.AddDays(1);
??????????????????????? loginNameCookie.Expires = DateTime.Now.AddDays(1);
??????????????????????? Response.Cookies.Add(loginNameCookie);
??????????????????????? Response.Cookies.Add(loginPassCookie);
??????????????????????? if (chkLogin.Checked == true)//自動登錄
??????????????????????? {
??????????????????????????? HttpCookie Login = new HttpCookie("Bic_LoginAuto", "true");
??????????????????????????? Login.Expires = DateTime.Now.AddDays(1);
??????????????????????????? Response.Cookies.Add(Login);
??????????????????????? }
??????????????????????? else
??????????????????????? {
??????????????????????????? HttpCookie Login = Request.Cookies["Bic_LoginAuto"];
??????????????????????????? if (Login != null)
??????????????????????????? {
??????????????????????????????? Login.Expires = DateTime.Now.AddDays(-2);
??????????????????????????????? Response.Cookies.Set(Login);
??????????????????????????? }
??????????????????????? }
??????????????????? }
??????????????????? else//沒選記住密碼
??????????????????? {
??????????????????????? HttpCookie loginNameCookie = Request.Cookies["Bic_LoginName"];
??????????????????????? HttpCookie loginPassCookie = Request.Cookies["Bic_Pass"];
??????????????????????? if (loginNameCookie != null)
??????????????????????? {
??????????????????????????? loginNameCookie.Expires = DateTime.Now.AddDays(-2);
??????????????????????????? Response.Cookies.Set(loginNameCookie);
??????????????????????? }
??????????????????????? if (loginPassCookie != null)
??????????????????????? {
??????????????????????????? loginPassCookie.Expires = DateTime.Now.AddDays(-2);
??????????????????????????? Response.Cookies.Set(loginPassCookie);
??????????????????????? }
??????????????????? }
??????????????????? Page.Session["Login_Name"] = user.Login_Name;
??????????????????? HttpContext.Current.Session["Display_Name"] = SysUserBLL.getDisplayNameByname(user.Login_Name);
??????????????????? Response.Write("<script>window.location='Default.aspx';</script>");
??????????????? }
??????????????? else
??????????????? {
??????????????????? Response.Write("<script>alert('用戶名或密碼錯誤!');window.location='Login.aspx';</script>");
??????????????? }
??????????? }
??????????? else
??????????? {
??????????????? Response.Write("<script>alert('請輸入賬號和密碼!');window.location='Login.aspx';</script>");
??????????? }
??????? }
以上的注釋只是我個人的一些思路和理解,如有不正確之處,還望大牛指導指導啊。如覺得文章對你有幫助,請多多支持,你們的支持將會是我寫博客的動力。
?
?
?
轉載于:https://www.cnblogs.com/zknu/p/3359303.html
總結
以上是生活随笔為你收集整理的业务系统实现记住密码和自动登录功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大话设计模式--建造者模式 Builde
- 下一篇: 解决Windows7 Embedded连