zoukankan      html  css  js  c++  java
  • asp.net记住我功能

    登录页面的记住我功能  
    不能用session的原因:sessionID是以cookie的形式存在浏览器端的内存中  如果用户把浏览器关闭 则sessionID就消失    
    但是服务器端的session在过期时间内还是存在的 等到浏览器在 默认的过期时间内(20分钟)不在向服务器发送请求 则过了20分钟 session销毁!
    前端简单模拟:
     1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="RememberMe.Login" %>
     2 
     3 <!DOCTYPE html>
     4 
     5 <html xmlns="http://www.w3.org/1999/xhtml">
     6 <head runat="server">
     7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     8     <title></title>
     9     <script type="text/javascript">
    10         window.onload = function () {
    11             document.getElementById('btnClose').onclick = function () {
    12                 window.close();
    13             };
    14         };
    15     </script>
    16 </head>
    17 <body>
    18     <form id="form1" runat="server">
    19         <div style="text-align: center;">
    20             <table>
    21                 <tr>
    22                     <td>用户名:
    23                         <input type="text" name="txtName" value="<%=uName %>" /></td>
    24                 </tr>
    25                 <tr>
    26                     <td>&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" name="txtPwd" value="<%=pwd %>" />
    27                     </td>
    28                 </tr>
    29                 <tr>
    30                     <td colspan="2">
    31                         <input type="checkbox" name="rememberMe" value="1" checked="checked" />记住我</td>
    32                 </tr>
    33                 <tr>
    34                     <td colspan="2">
    35                         <input type="submit" value="登录" />
    36                         <input type="button" value="关闭" id="btnClose" /></td>
    37                 </tr>
    38             </table>
    39 
    40         </div>
    41     </form>
    42 </body>
    43 </html>
    Login.aspx

    后台代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.UI;
     6 using System.Web.UI.WebControls;
     7 
     8 namespace RememberMe
     9 {
    10     public partial class Login : System.Web.UI.Page
    11     {
    12         protected string uName;
    13         protected string pwd;
    14         protected void Page_Load(object sender, EventArgs e)
    15         {
    16 
    17             if (Request.Cookies["user"] != null)
    18             {
    19                 uName = Request.Cookies["user"].Values["n"];
    20                 pwd = Request.Cookies["user"].Values["p"];
    21             }
    22             if (IsPostBack)
    23             {
    24                 string userName = Request.Form["txtName"];
    25                 string userPwd = Request.Form["txtPwd"];
    26                 if (!string.IsNullOrEmpty(Request.Form["rememberMe"]))
    27                 {
    28                     if (userName == "admin" && userPwd == "admin")
    29                     {
    30                         AlertAndRedirect("Index.aspx?n=" + userName, "登录成功");
    31                         HttpCookie cookie = new HttpCookie("user");
    32                         cookie["n"] = userName;
    33                         cookie["p"] = userPwd;
    34                         cookie.Expires = DateTime.Now.AddDays(7);
    35                         Response.Cookies.Add(cookie);
    36                     }
    37                     else
    38                     {
    39                         AlertAndRedirect("Login.aspx", "登录失败");
    40                         Response.Cookies["user"].Expires = DateTime.Now.AddDays(-1);
    41                     }
    42                 }
    43                 else
    44                 {
    45                     Response.Cookies["user"].Expires = DateTime.Now.AddDays(-1);
    46                     if (userName == "admin" && userPwd == "admin")
    47                     {
    48                         AlertAndRedirect("Index.aspx?n=" + userName, "登录成功");
    49                     }
    50                     else
    51                     {
    52                         AlertAndRedirect("Login.aspx", "登录失败");
    53                     }
    54                 }
    55             }
    56 
    57         }
    58         private void AlertAndRedirect(string redirectURL, string msg)
    59         {
    60             Response.Write("<script>alert('" + msg + "');window.location.href='" + redirectURL + "';</script>");
    61         }
    62     }
    63 }
    Login.aspx,cs


    基本功能实现。下载:http://www.cnblogs.com/wolf-sun/admin/Files.aspx

  • 相关阅读:
    cocos2dx 3.x 集成protobuf
    Lua面向对象之三:其它一些尝试
    Lua面向对象之二:类继承
    Lua面向对象之一:简单例子
    cocos2dx lua 绑定之二:手动绑定自定义类中的函数
    向量点积、叉积的意义
    cocos2dx lua 绑定之一:自动绑定自定义类中的函数
    Lua和C++交互 学习记录之九:在Lua中以面向对象的方式使用C++注册的类
    Lua和C++交互 学习记录之八:C++类注册为Lua模块
    Lua和C++交互 学习记录之七:C++全局函数注册为Lua模块
  • 原文地址:https://www.cnblogs.com/wolf-sun/p/3217504.html
Copyright © 2011-2022 走看看