asp_完美登录

来源:互联网 发布:腾讯位置大数据 编辑:程序博客网 时间:2024/05/04 21:00

WebForm1.aspx:

 <form id="form1" runat="server">
    <div>
    <% Response.Write(Application["UserNum"]); %>
    </div>
    </form>


Login.aspx:

<script src="../js/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        $(function () {
            
            <%= ScriptString %>


            $("#vCode").click(function() {


                var str = $(this).attr("src") + 1;
                
                $(this).attr("src",str);
            });
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>用户名:</td>
                    <td>
                        <input type="text" name="txtLongInName" value="<%= LoginName %>" /></td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td>
                        <input type="text" name="txtPwd" /></td>
                </tr>
                 <tr>
                    <td>验证码:</td>
                    <td>
                       <img id="vCode" src="ShowValidateCodeImage.ashx?id=1"/></td>
                </tr>
                  <tr>
                    <td>验证码:</td>
                    <td>
                        <input type="text" name="txtValdiateCode" /></td>
                </tr>
                <tr>
                    <td rowspan="2">
                        <input type="submit" value="登录" /></td>
                </tr>


            </table>


        </div>
    </form>
</body>


Login.aspx.cs:

 public partial class Login : System.Web.UI.Page
    {
        public string LoginName { get; set; }


        public string ScriptString { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //第一次请求来。从Cookie当中拿到当前的登录成功用户名,然后放到前台去。
                string lastLoginUserName = Request["loginName"];
                LoginName = lastLoginUserName;
            }
            else
            {
                //处理登录


                //处理验证码
                //之前发送给你的验证码的字符串和你提交回来的是否一致。
                string postVCode= Request["txtValdiateCode"];
              
                string sessionVCode = Session["VCode"] == null ? string.Empty : Session["VCode"].ToString();


                if (string.IsNullOrEmpty(sessionVCode))
                {
                    ScriptString = "alert('验证码错误');";
                    return;
                }


                Session["VCode"] = null;


                if (postVCode != sessionVCode)
                {
                    //王前台输出一段脚本。
                    //这么做都是没有经验小小鸟。
                    //Response.Write("<script>alert('验证码错误')</script>");


                    //一般鸟
                    //ClientScript.RegisterClientScriptBlock(this.GetType(),"ss","<script>alert('验证码错误')</script>");


                    ScriptString = "alert('验证码错误');";
                    return;
                }


                //处理校验用户名密码
                string name = Request["txtLongInName"];
                string pwd = Request["txtPwd"];
                BLL.HKSJ_USERS userService =new HKSJ_USERS();


                List<Model.HKSJ_USERS> userList = userService.GetModelList(" UserName='" + name + "' and loginname='" + pwd + "' ");


                if (userList.Count <= 0)
                {
                    ScriptString = "alert('登录败了!');";
                    return;
                }


                Response.Cookies["loginName"].Value = userList.FirstOrDefault().UserName;
                Response.Cookies["loginName"].Expires = DateTime.Now.AddDays(7);


                //登录成功之后跳转之前。
                Session["LoginUser"] = userList.FirstOrDefault();


                Response.Redirect("Main.aspx");


            }
        }
    }


Main.aspx:

<body>
    <form id="form1" runat="server">
        <asp:Button ID="btnLogOut" runat="server" Text="退出" OnClick="btnLogOut_Click" />
    <div>
    <h1>欢迎进入  系统</h1>
    </div>
    </form>
</body>


Main.aspx.cs:

  protected void btnLogOut_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("Login.aspx");
            //Session.Abandon();
        }


ShowValited.ashx.cs:

 /// <summary>
    /// ShowValidateCodeImage 的摘要说明
    /// 实现IRequiresSessionState接口,才会加载Session。
    /// 微软为了提高一般处理程序的性能,默认没有加载Session
    /// </summary>
    public class ShowValidateCodeImage : IHttpHandler,IRequiresSessionState
    {


        public void ProcessRequest(HttpContext context)
        {
            //数据存储服务器端。它是针对所有用户的。
            //context.Application["sss"] = "sssss";




            Common.ValidateCode validateCode =new Common.ValidateCode();
            string str = validateCode.CreateValidateCode(4);
            validateCode.CreateValidateGraphic(str,context);


            //把验证码放到Session
            context.Session["VCode"] = str;
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }