asp.net记住我功能
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="RememberMe.Login" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
window.onload = function () {
document.getElementById('btnClose').onclick = function () {
window.close();
};
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center;">
<table>
<tr>
<td>用户名:
<input type="text" name="txtName" value="<%=uName %>" /></td>
</tr>
<tr>
<td>密 码:<input type="password" name="txtPwd" value="<%=pwd %>" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="checkbox" name="rememberMe" value="1" checked="checked" />记住我</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登录" />
<input type="button" value="关闭" id="btnClose" /></td>
</tr>
</table> </div>
</form>
</body>
</html>
Login.aspx
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace RememberMe
{
public partial class Login : System.Web.UI.Page
{
protected string uName;
protected string pwd;
protected void Page_Load(object sender, EventArgs e)
{ if (Request.Cookies["user"] != null)
{
uName = Request.Cookies["user"].Values["n"];
pwd = Request.Cookies["user"].Values["p"];
}
if (IsPostBack)
{
string userName = Request.Form["txtName"];
string userPwd = Request.Form["txtPwd"];
if (!string.IsNullOrEmpty(Request.Form["rememberMe"]))
{
if (userName == "admin" && userPwd == "admin")
{
AlertAndRedirect("Index.aspx?n=" + userName, "登录成功");
HttpCookie cookie = new HttpCookie("user");
cookie["n"] = userName;
cookie["p"] = userPwd;
cookie.Expires = DateTime.Now.AddDays();
Response.Cookies.Add(cookie);
}
else
{
AlertAndRedirect("Login.aspx", "登录失败");
Response.Cookies["user"].Expires = DateTime.Now.AddDays(-);
}
}
else
{
Response.Cookies["user"].Expires = DateTime.Now.AddDays(-);
if (userName == "admin" && userPwd == "admin")
{
AlertAndRedirect("Index.aspx?n=" + userName, "登录成功");
}
else
{
AlertAndRedirect("Login.aspx", "登录失败");
}
}
} }
private void AlertAndRedirect(string redirectURL, string msg)
{
Response.Write("<script>alert('" + msg + "');window.location.href='" + redirectURL + "';</script>");
}
}
}
Login.aspx,cs
asp.net记住我功能的更多相关文章
- java实现记住密码功能(利用cookie)
<br> <input type="text" id="userName" name="userName" value=& ...
- 通过sharedpreferences实现记住密码功能
通过sharedpreferences实现记住密码功能
- jquery.cookie.js 操作cookie实现记住密码功能的实现代码
jquery.cookie.js操作cookie实现记住密码功能,很简单很强大,喜欢的朋友可以参考下. 复制代码代码如下: //初始化页面时验证是否记住了密码 $(document).ready( ...
- cookie记住密码功能
很多门户网站都提供了记住密码功能,虽然现在的浏览器都已经提供了相应的记住密码功能 效果就是你每次进入登录页面后就不需要再进行用户名和密码的输入: 记住密码功能基本都是使用cookie来进行实现的,因此 ...
- 【原创】js中利用cookie实现记住密码功能
在登录界面添加记住密码功能,我首先想到的是在java后台中调用cookie存放账号密码,大致如下: HttpServletRequest request HttpServletResponse res ...
- android: SharedPreferences实现记住密码功能
既然是实现记住密码的功能,那么我们就不需要从头去写了,因为在上一章中的最佳实 践部分已经编写过一个登录界面了,有可以重用的代码为什么不用呢?那就首先打开 BroadcastBestPractice 项 ...
- asp.net(C#)实现功能强大的时间日期处理类完整实例
作者:smartsmile2012 字体:[增加 减小] 类型:转载 时间:2016-06-30我要评论 这篇文章主要介绍了asp.net(C#)实现功能强大的时间日期处理类,封装了针对日期与时间的各 ...
- vue项目实现记住密码功能
一.谷歌浏览的残留问题 现在很多的网站都有一个需求是记住密码这个功能,为的是避免用户下次登录的时候繁琐的输入过程. 像是一些主流的浏览器(比如Chrome)都有了这个功能,而且如果你登录了Chrom ...
- SpringBoot + Spring Security 学习笔记(四)记住我功能实现
记住我功能的基本原理 当用户登录发起认证请求时,会通过UsernamePasswordAuthenticationFilter进行用户认证,认证成功之后,SpringSecurity 调用前期配置好的 ...
随机推荐
- Caffe学习系列(12):训练和测试自己的图片
学习caffe的目的,不是简单的做几个练习,最终还是要用到自己的实际项目或科研中.因此,本文介绍一下,从自己的原始图片到lmdb数据,再到训练和测试模型的整个流程. 一.准备数据 有条件的同学,可以去 ...
- GEOS库在windows中的编译和测试(vs2012)
版本:vs2012, geos3.5 一.下载和编译 这类的文章比较,不再具体细说,可以参考 http://blog.csdn.net/wangqinghao/article/details/8201 ...
- livewriter写Blog 神秘失踪?
现在习惯用livewriter来总结/记录一些知识并发布为Blog 与同行交流,但是今天发生了一个怪事,上午我整理了两篇文档当时就用livewriter发送到了Blog上,但是晚上来看的时候之前发送的 ...
- UIView的layoutSubviews和drawRect方法何时调用 ———转
转自:http://jianyu996.blog.163.com/blog/static/112114555201305113018814/ 首先两个方法都是异步执行.layoutSubviews方便 ...
- 云计算之路-阿里云上:2014年6月12日16:14遭遇SLB故障
这是今天遇到的第3次故障,故障是首先被园友发现的,访问园子出现来自SLB的错误提示:400 Bad Request - Powered by Tengine. 查看Windows性能监视器发现,在故障 ...
- gulp初体验记录(简介、插件开发介绍)
目前用的业界比较知名的三个前端构建工具:grunt.gulp.fis,自己此前一直都是只在用grunt,fis看过一点,gulp则一直都没注意过,直到最近发现好像用的人越来越多,所以今天也就抽了点时间 ...
- 04.C#类型系统、值类型和引用类型(二章2.2-2.3)
今天要写的东西都是书中一些概念性的东西,就当抄笔记,以提问对话的方式将其写出来吧,说不定以后面试能有点谈资~~~ Q1.C#1系统类型包含哪三点特性? A1.C#1类型系统是静态的.显式的和安全的. ...
- [AaronYang]C#人爱学不学[5]
这世上有三样东西是别人抢不走的:一是吃进胃里的食物,二是藏在心中的梦想,三是读进大脑的书 --Aaronyang的博客(www.ayjs.net) 1. 数组-的疑惑? 1.1 多维数组 ...
- 标准IDispose模式浅析
DoNet资源 众所周知,.Net内存管理分托管资源和非托管资源,把内存中的对象按照这两种资源划分,然后由GC负责回收托管资源(Managed Resource),而对于非托管资源来讲,就需要程序员手 ...
- 解决 SQL Server Profiler 跟踪[不断]出现检索数据
问题简单回顾: 当我们使用SQL Server Profiler根据数据时,有时刚打开什么也没干呢,就显示很多数据了,当我们用橡皮擦清除,没过两秒就又有了,如图: 是不是很恼火!~不怕,解决方案如下: ...