关于form验证的处理片断
public virtual void SignIn(s_User user, bool createPersistentCookie)
{
var now = DateTime.UtcNow.ToLocalTime(); //01 实例化一个form表单身份验证票证
//FormsAuthenticationTicket(int version, string name, DateTime issueDate, DateTime expiration, bool isPersistent, string userData, string cookiePath);
var ticket = new FormsAuthenticationTicket(
/*version*/, //票证的版本号
user.Nickname, //用户名
now, //发生时间
now.Add(_expirationTimeSpan), //过期时间,通常用FormsAuthentication.Timeout作默认值
createPersistentCookie, //true存储在cookie,false存储在url,这个用户选择,“记住我”
user.Email, //用户数据,这里只保存了email
FormsAuthentication.FormsCookiePath); //Cookie存放路径,通常用FormsAuthentication.FormsCookiePath作默认值 //02 将票证加密成适合cookie保存的字符串
string encryptedTicket = FormsAuthentication.Encrypt(ticket); //03 将加密后的字符串写入cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
cookie.HttpOnly = true; //不允许客户端脚本访问cookie,仅限http访问
if (ticket.IsPersistent)//如果票证需要持久化,指定cookie过期时间
{
cookie.Expires = ticket.Expiration;
}
//使用https传输此cookie
cookie.Secure = FormsAuthentication.RequireSSL;
cookie.Path = FormsAuthentication.FormsCookiePath;
if (FormsAuthentication.CookieDomain != null)
{
cookie.Domain = FormsAuthentication.CookieDomain;
} _httpContext.Response.Cookies.Add(cookie);
_cachedUser = user; //如果想用默认的方式处理,不写上面那么多:FormsAuthentication.SetAuthCookie(loginName, true);
} //验证当前通过验证的用户
public virtual s_User GetAuthenticatedUser()
{
//如果有缓存的用户,就返回缓存的用户 //如果是基于http会话级的生命周期注入方式,则是可以这样写的
if (_cachedUser != null) return _cachedUser; if (_httpContext == null || //如果httpContext为空
_httpContext.Request == null || // 或httpContext.Request为空 Request.IsAuthenticated为假就返回 空
!_httpContext.Request.IsAuthenticated || // 或或httpContext.Request.IsAuthenticated = false
!(_httpContext.User.Identity is FormsIdentity)) //_httpContext.User.Identity的票证不是 FormsIdentity
{
return null; // 都返回null, 即用户验证失败
} //获取会话中的表单身份验证票证[这个user封装了读cookie,解密cookie,验证转换的过程]
var formsIdentity = (FormsIdentity)_httpContext.User.Identity; //从formsIdentity.Ticket.UserData取email
var userEmail = formsIdentity.Ticket.UserData; //如果email验证失败,则验证失败
if (String.IsNullOrWhiteSpace(userEmail)) return null; //用email去查询数据库,获取user
var user = _userService.GetUserByEmail(userEmail); //如果是合法用户,返回当前合法用户
if (user != null && user.Active ) _cachedUser = user;
return user;
} public virtual void SignOut()
{
_cachedUser = null;
FormsAuthentication.SignOut();
}
关于form验证的处理片断的更多相关文章
- Nodejs之MEAN栈开发(四)---- form验证及图片上传
这一节增加推荐图书的提交和删除功能,来学习node的form提交以及node的图片上传功能.开始之前需要源码同学可以先在git上fork:https://github.com/stoneniqiu/R ...
- Asp.Net Form验证不通过,重复登录
问题产生根源: 当然,其实应该需要保持线上所有机器环境一致!可是,写了一个小程序.使用的是4.5,aysnc/await实在太好用了,真心不想把代码修改回去. so,动了念头,在这台服务器上装个4.5 ...
- tornado web高级开发项目之抽屉官网的页面登陆验证、form验证、点赞、评论、文章分页处理、发送邮箱验证码、登陆验证码、注册、发布文章、上传图片
本博文将一步步带领你实现抽屉官网的各种功能:包括登陆.注册.发送邮箱验证码.登陆验证码.页面登陆验证.发布文章.上传图片.form验证.点赞.评论.文章分页处理以及基于tornado的后端和ajax的 ...
- form验证及图片上传
form验证及图片上传 这一节增加推荐图书的提交和删除功能,来学习node的form提交以及node的图片上传功能.开始之前需要源码同学可以先在git上fork:https://github.com/ ...
- angular实现form验证
先上效果页面:https://lpdong.github.io/myForm-1/ 其中几个知识点 1.angularJs提供了几个新的type类型: type="password" ...
- Python Django的分页,Form验证,中间件
本节内容 Django的分页 Form 中间件 1 Django 分页 1.1 Django自带的分页 1.首先来看下我的测试数据环境 ############ models.py ######### ...
- Django中Form验证
Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 一,Form验证 第一种操作:主要是这三个函数 ...
- Django form验证
# 模版 class LoginForm(forms.Form): # 模版中的元素 user = forms.CharField(min_length=6,error_messages={" ...
- python自动化开发-[第二十一天]-form验证,中间件,缓存,信号,admin后台
今日概要: 1.form表单进阶 2.中间件 3.缓存 4.信号 5.admin后台 上节课回顾 FBV,CBV 序列化 - Django内置 - json.dumps(xxx,cls=) Form验 ...
随机推荐
- Oracle创建、删除表空间、用户
1.创建临时表空间 create temporary tablespace linshi tempfile 'e:\linshi.dbf' size 50m autoextend on next 50 ...
- Linux下安装php加速器xcache
一.环境说明 php安装目录:/usr/local/php php.ini配置文件路径:/usr/local/php/etc/php.ini Nginx安装目录:/usr/local/nginx Ng ...
- 5、StringBuffer与StringBuilder的区别(十进制转二进制)
1. 在执行速度方面的比较:StringBuilder > StringBuffer 2. StringBuffer与StringBuilder,他们是字符串变量,是可改变的对象,每当我们用它们 ...
- div+css样式
Div+Css 随着页面上的需求变大,许多的东西不再使用单纯的图片.按钮.文字,而是通过Div+Css来实现按钮,公司的需求就是这样,一直在弄这个模块,顺便的总结一下 列如下面的页面都是通过div+c ...
- Hbase客户端API基础小结笔记(未完)
客户端API:基础 HBase的主要客户端接口是由org.apache.hadoop.hbase.client包中的HTable类提供的,通过这个类,用户可以完成向HBase存储和检索数据,以及删除无 ...
- DDL DML DCL语句
总体解释:DML(data manipulation language):自动提交的数据库操作语言 它们是SELECT.UPDATE.INSERT.DELETE,就象它的名字一样 DDL( ...
- 整理: Android HAL
这篇文章整理来自http://bbs.chinaunix.net/thread-3675980-1-1.html 在论坛中看到的Android HAL讨论,有个ID描述的比较清楚,摘录如下: temp ...
- 【原】安装mongo的php插件
http://pecl.php.net/package/mongo https://github.com/mongodb/mongo-php-driver/tarball/master 1. 安装mo ...
- Thinkpad 笔记本VMware Workstation 安装虚拟机出现“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”解决方法
今天在使用VMware打算在机器中安装新的虚拟机时,出现"此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态"错误如下: 提示信息: 已将该虚拟机配 ...
- 用webview打开网页时,里面有个div带滚动条的,但是在平板上滚动条失效
android2.3的不支持滚动条,并且scrollTop也不支持的.(设置overflow未hidden就可以支持). function noBarsOnTouchScreen(arg) { var ...