FormsAuthenticationTicket
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security; namespace WebAppTest.Controllers
{
public class TestController : Controller
{
// GET: Test
public ActionResult Index()
{
return View();
} /// <summary>
/// 模拟登陆
/// 票据中的数据经过加密,解决了cookie的安全问题。
/// </summary>
/// <param name="username"></param>
public ActionResult Login()
{
WebUserData User = new WebUserData()
{
UserName = "wolf",
Age = ,
Area =
};
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(, User.UserName, DateTime.Now, DateTime.Now.AddMinutes(), false, Newtonsoft.Json.JsonConvert.SerializeObject(User));
string encTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie newCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)
{
HttpOnly = true,
Secure = FormsAuthentication.RequireSSL,
Domain = FormsAuthentication.CookieDomain,
Path = FormsAuthentication.FormsCookiePath
};
Response.Cookies.Add(newCookie);
return RedirectToAction("Index");
}
/// <summary>
/// 退出登录
/// </summary>
public ActionResult Logout()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index");
}
/// <summary>
/// 取得票据中数据
/// </summary>
/// <returns></returns>
[HttpPost]
public ActionResult GetUserData()
{
HttpCookie cookie = HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
return Json(Newtonsoft.Json.JsonConvert.DeserializeObject<WebUserData>(ticket.UserData));
}
} public class WebUserData
{
public string UserName { get; set; }
public int Age { get; set; }
public int Area { get; set; } } }
FormsAuthenticationTicket的更多相关文章
- asp.net 登陆验证 Form表单验证的3种方式 FormsAuthentication.SetAuthCookie;FormsAuthentication.RedirectFromLoginPage;FormsAuthenticationTicket
我们在登陆成功后,使用下面的3种方法,都是同一个目的:创建身份验证票并将其附加到 Cookie, 当我们用Forms认证方式的时候,可以使用HttpContext.Current.User.Ident ...
- 经典FormsAuthenticationTicket 分析
Asp.net中基于Forms验证的角色验证授权 Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验证用的最多, ...
- ASP.NET的票据工具类FormsAuthenticationTicket
票据是asp.net登录验证的一种方式,以前研究过,现在并不使用,今天发现了,记录一下. /*###################票据工具################### * 1.设置< ...
- FormsAuthenticationTicket学习笔记
FormsAuthenticationTicket ticket = , ), true, string.Format("{0}:{1}", "username" ...
- RedirectFromLoginPage和FormsAuthenticationTicket的区别
如果你对.net身份验证不是很清晰,请看本文.本文用简单明了的语言,让你对RedirectFromLoginPage和FormsAuthenticationTicket有一个完整的认识. 1)Form ...
- .net mvc结合微软提供的FormsAuthenticationTicket登陆
一.Web.config <system.web> <compilation debug="true" targetFramework="4.5&quo ...
- 基于ASP.MVC票据FormsAuthenticationTicket身份认证
做一个最基础的业务需求用户登录,将此用户的身份发回到客户端的Cookie,之后此用户再访问这个web应用就会连同这个身份Cookie一起发送到服务端.服务端上的授权设置就可以根据不同目录对不同用户的访 ...
- 使用FormsAuthenticationTicket进行登陆验证
if (账号密码验证成功) { //登陆成功 Session["User"] = account; FormsAuthenticationTicket ticket = new F ...
- asp.net登录验证FormsAuthenticationTicket和FormsAuthentication类
登录部分使用的类 FormsAuthentication 为 Web 应用程序管理 Forms 身份验证服务. 配置启用身份验证,WEB.config配置: <system.web> ...
随机推荐
- FAT文件系统规范v1.03学习笔记---2.FAT区之FAT数据结构(Fat Data Structure)
1.前言 本文主要是对Microsoft Extensible Firmware Initiative FAT32 File System Specification中文翻译版的学习笔记. 每个FAT ...
- vue生命周期学习(watch跟computed)
1.watch钩子函数监听数据的变化 watch 的一个特点是,最初绑定的时候是不会执行的,要等到 firstName 改变时才执行监听计算. <div> <p>FullNam ...
- ansible 常见指令表
Play 指令 说明 accelerate 开启加速模式 accelerate_ipv6 是否开启ipv6 accelerate_port 加速模式的端口 always_run any_error ...
- English常用短语
(1) be waken by 被什么吵醒 (2) wake up ! 快醒醒 (3) put the flames /fleimz/ ...
- git与eclipse集成之导入组件到Eclipse工程
从工作目录中选择要导入的组件,右键选择:Import Projects,弹出窗口如下图所示,选择Import as general project 点击next,修改或使用默认的组件名称 点击fini ...
- MinGW GCC 7.1.0 2017年6月份出炉啦
MSYS_MinGW-w64_GCC_710_x86-x64_Full.7z 发布日期: 2017-06-03 18:33 69.4M 下载地址: http://xhmikosr.1f0.de/too ...
- LuoGu P1352 没有上司的舞会
题目传送门 这可能是最简单的树形Dp了吧 对于每个人,要么他来,他的下属不来 要么他不来,他的下属爱来不来 于是设计状态: f[i][0/1]表示以i为根的子树中最大能达到的快乐值(i这个人选或者不选 ...
- Confluence 6 新 Confluence 安装配置一个数据源连接
如果在你的 Tomcat 中配置了数据源,并且Confluence 设置指南在安装的时候检测到这个配置的时候,配置数据源的选项将会提供给你进行配置.入股你希望使用数据源,请参考下面的配置. 1. 停止 ...
- Java测试的题目感想
日期:2018.9.24 星期一 博客期:012 说起来测试真的是来的时候信心满满,考完的时候慌得出神!我感觉自己会用Scanner类做输出和文件操作就可以在有限时间内把它搞出来了!事实证明我错了!我 ...
- 扇形多级菜单可配置Demo
预览效果 领导是想把这个做成复选框,所以做成了可以多选的... Demo下载:https://github.com/zhangzn3/arc-menu