ASP.NET Forms验证
/// <summary>
/// 执行用户登录操作
/// </summary>
/// <param name="config">授权配置信息</param>
/// <param name="userData">与登录名相关的用户信息</param>
/// <param name="expiration">登录Cookie的过期时间,单位:分钟,默认120分钟。</param>
public static void SignIn(IovAuthConfig config, UserInfo userData, int expiration = 120)
{
if (config == null)
throw new ArgumentNullException("config");
if (userData == null)
throw new ArgumentNullException("userData");
if(string.IsNullOrWhiteSpace(config.AppID))
throw new ArgumentNullException("AppID");
// 1. 把需要保存的用户数据转成一个字符串。
string data = null;
if (userData != null)
data = JsonHelper.Serialize(userData); // 2. 创建一个FormsAuthenticationTicket,它包含登录名以及额外的用户数据。
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
2, userData.LoginID, DateTime.Now, DateTime.Now.AddDays(1), true, data); // 3. 加密Ticket,变成一个加密的字符串。
string cookieValue = FormsAuthentication.Encrypt(ticket); // 4. 根据加密结果创建登录Cookie
HttpCookie cookie = new HttpCookie(config.AppID, cookieValue);
cookie.HttpOnly = true;
cookie.Secure = FormsAuthentication.RequireSSL;
cookie.Domain = FormsAuthentication.CookieDomain;
cookie.Path = FormsAuthentication.FormsCookiePath;
//if (expiration > 0)
//默认过期时间:120分钟
cookie.Expires = DateTime.Now.AddMinutes(expiration == 0 ? 120 : expiration); HttpContext context = HttpContext.Current;
if (context == null)
throw new InvalidOperationException(); // 5. 写登录Cookie
context.Response.Cookies.Remove(cookie.Name);
context.Response.Cookies.Add(cookie);
}
web.config同时需要修改两个地方,如下:
<system.web>
<authentication mode="Forms">
<forms name="IOV.Test" loginUrl="/" protection="All" timeout="43200" path="/" domain="" requireSSL="false" slidingExpiration="true" />
</authentication>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"></modules>
</system.webServer>
获取已登录用户信息:
/// <summary>
/// 获取当前用户信息
/// </summary>
/// <param name="context">当前Http请求上下文</param>
/// <returns></returns>
public static UserInfo TryGetUserInfo(HttpContext context)
{
if (context == null)
throw new ArgumentNullException("context"); // 1. 读登录Cookie
HttpCookie cookie = context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (cookie == null || string.IsNullOrEmpty(cookie.Value))
return null; try
{
UserInfo userData = null;
// 2. 解密Cookie值,获取FormsAuthenticationTicket对象
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value); if (ticket != null && string.IsNullOrEmpty(ticket.UserData) == false)
// 3. 还原用户数据
userData = JsonHelper.Desrialize<UserInfo>(ticket.UserData); return userData;
}
catch { /* 有异常也不要抛出,防止攻击者试探。 */ }
return null;
}
ASP.NET Forms验证的更多相关文章
- 当ASP.NET Forms验证方式遭遇苹果IOS
一.问题出现 我在用ASP.NET MVC4做微信开发的时候,用Forms验证方式做为authentication. 一般都是在web.config加: <authentication mode ...
- Asp.Net的Forms验证,解决Cookie和Seesion失效时间
网站开发中用户验证一般采用Asp.Net的Forms验证,验证票据存储到Cookie的方式. Session方式是将验证信息存储在内存中,如果你使用的虚拟主机给你分配很小的内存,实际上都是如此,那么s ...
- C# ASP.NET Forms身份认证
原文:https://www.cnblogs.com/kyo-lynn/p/3418577.html 原文:https://www.cnblogs.com/fish-li/archive/2012/0 ...
- asp.net Forms登录核心方法
登录核心方法: private void Signin(string curUserId) { System.Web.Security.FormsAuthenticationTicket tk = , ...
- ASP.NET Forms 身份验证
ASP.NET Forms 身份验证 在开发过程中,我们需要做的事情包括: 1. 在 web.config 中设置 Forms 身份验证相关参数.2. 创建登录页. 登录页中的操作包括: 1. 验证用 ...
- 【MVC】ASP.NET MVC Forms验证机制
http://www.cnblogs.com/bomo/p/3309766.html 随笔 - 121 文章 - 0 评论 - 92 [MVC]ASP.NET MVC Forms验证机制 ASP. ...
- asp.net中使用基于角色role的Forms验证
http://www.cnblogs.com/yao/archive/2006/06/24/434783.html asp.net中使用基于角色role的Forms验证,大致经过几下四步:1.配置系统 ...
- [转]ASP.NET中的forms验证
本文转自:http://www.cnblogs.com/fengzheng126/archive/2012/04/06/2435513.html ASP.NET的安全认证:Windows验证 (默认) ...
- 给Asp.net MVC Forms 验证设置角色访问控制
当我们使用Asp.net MVC Forms方式验证用户, 然后设置Controller 或 Action 的 Authorize属性时, 默认情况下只有Users属性可以设置(这里的Users通常是 ...
随机推荐
- Cannot uninstall 'pyserial'. It is a distutils installed project and thus we cannot a ccurately determine which files belong to it which would lead to only a partial uninstall. 解决方法
最近再升级 pyserial模块时,采用 pip install --upgrade pyserial,待模块下载完成准备卸载原版本时 提示:“Cannot uninstall 'pyserial'. ...
- rman 脚本大全
################################################################一个增量备份的例子脚本######################### ...
- 敌兵布阵 HDU - 1166 (树状数组模板题,线段树模板题)
思路:就是树状数组的模板题,利用的就是单点更新和区间求和是树状数组的强项时间复杂度为m*log(n) 没想到自己以前把这道题当线段树的单点更新刷了. 树状数组: #include<iostrea ...
- gulp学习-metamask前端使用
https://www.gulpjs.com.cn/docs/getting-started/ ,这个是3.9.0版本 后面发现安装的版本是4.0.0,看下面这个: https://github.co ...
- 理解 boxsizing
理解boxsizing 什么是css盒模型?css盒模型包括如下属性:内容(content),填充(padding),边框(border),边界(margin). 这些东西我们可以拿日常生活中的列子来 ...
- linux下模拟一个木马程序运行过程
预备知识: 将一个程序放入到后台,悄悄的执行 ./xxx.sh & 进程: 用户进程:由用户来管理 系统进程:由系统内核自行管理 系统中的每个进程,都有一个位置的ID,这就是pid,而且每次启 ...
- MySQL(一)MySQL基础介绍
最近的学习内容是数据库相关的一些知识,主要以MySQL为主,参考书籍——<MySQL必知必会> MySQL学习及下载地址:https://dev.mysql.com/ MySQL学习使用注 ...
- rook 排错记录 + Orphaned pod found kube-controller-manager的日志输出
1.查看rook-agent(重要)和mysql-wordpress 的日志,如下: MountVolume.SetUp failed for volume "pvc-f002e1fe-46 ...
- security相关链接整理
token令牌 ssl协议 https协议 对称加密与非对称加密 认识ASP.NET Windows身份认证
- CF101D Castle 树形DP、贪心
题目传送门 题意:给出一个有$N$个点的树,你最开始在$1$号点,经过第$i$条边需要花费$w_i$的时间.每条边只能被经过$2$次.求出到达除$1$号点外所有点的最早时间的最小平均值.$N \leq ...