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的更多相关文章

  1. asp.net 登陆验证 Form表单验证的3种方式 FormsAuthentication.SetAuthCookie;FormsAuthentication.RedirectFromLoginPage;FormsAuthenticationTicket

    我们在登陆成功后,使用下面的3种方法,都是同一个目的:创建身份验证票并将其附加到 Cookie, 当我们用Forms认证方式的时候,可以使用HttpContext.Current.User.Ident ...

  2. 经典FormsAuthenticationTicket 分析

    Asp.net中基于Forms验证的角色验证授权 Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验证用的最多, ...

  3. ASP.NET的票据工具类FormsAuthenticationTicket

    票据是asp.net登录验证的一种方式,以前研究过,现在并不使用,今天发现了,记录一下. /*###################票据工具################### * 1.设置< ...

  4. FormsAuthenticationTicket学习笔记

    FormsAuthenticationTicket ticket = , ), true, string.Format("{0}:{1}", "username" ...

  5. RedirectFromLoginPage和FormsAuthenticationTicket的区别

    如果你对.net身份验证不是很清晰,请看本文.本文用简单明了的语言,让你对RedirectFromLoginPage和FormsAuthenticationTicket有一个完整的认识. 1)Form ...

  6. .net mvc结合微软提供的FormsAuthenticationTicket登陆

    一.Web.config <system.web> <compilation debug="true" targetFramework="4.5&quo ...

  7. 基于ASP.MVC票据FormsAuthenticationTicket身份认证

    做一个最基础的业务需求用户登录,将此用户的身份发回到客户端的Cookie,之后此用户再访问这个web应用就会连同这个身份Cookie一起发送到服务端.服务端上的授权设置就可以根据不同目录对不同用户的访 ...

  8. 使用FormsAuthenticationTicket进行登陆验证

    if (账号密码验证成功) { //登陆成功 Session["User"] = account; FormsAuthenticationTicket ticket = new F ...

  9. asp.net登录验证FormsAuthenticationTicket和FormsAuthentication类

    登录部分使用的类 FormsAuthentication   为 Web 应用程序管理 Forms 身份验证服务. 配置启用身份验证,WEB.config配置: <system.web> ...

随机推荐

  1. Efuse--芯片存储

    1.Efuse是什么 Efuse类似于EEPROM,是一次性可编程存储器,在芯片出场之前会被写入信息,在一个芯片中,efuse的容量通常很小,一些芯片efuse只有128bit. 2.efuse的作用 ...

  2. linux下添加删除,修改,查看用户和用户组

    一.组操作 1.创建组: groupadd test #增加一个test组 2.修改组 groupmod -n test2 test #将test组的名子改成test2 3.删除组 groupdel ...

  3. 题解-Atcoder_agc005D ~K Perm Counting

    Problem AtCoder-agc005D 题意概要:给出\(n,k\),求合法的排列个数,其中合法定义为任何数字所在位置与自身值差的绝对值不为\(k\)(即求排列\(\{A_i\}\),使得\( ...

  4. (转!)Pyinstaller 打包发布经验总结

    原文地址 https://blog.csdn.net/weixin_42052836/article/details/82315118 具体的实现图待本人实现后贴上 原 Pyinstaller 打包发 ...

  5. Fiddler对https抓包时,提示"HTTPS decryption is disabled."

    安装了fiddlercertmaker.exe 后,对 https://www.baidu.com 进行抓包时,右侧界面提示"HTTPS decryption is disabled.&qu ...

  6. VS2017编译LevelDB

    环境: 操作系统:Win7 x64 编译器:VS2017 需要Boost库支持,需要先将Boost库编译成为64位版本. 一.项目文件导入 1. 下载leveldb-windows,https://c ...

  7. UML建模图

    UML 2.0规范 迅速成为建立软件系统可视化.规范.文档的标准.统一建模语言(UML) 也被用于非软件系统的建模,并在很多领域,诸如金融,军事,工程方面应用广泛. UML 2 定义了13种基本的图, ...

  8. Ex4_21 最短路径算法可以应用于货币交易领域..._第十二次作业

    (a)   建立一个有向图G(V,E),每个顶点表示一种货币,两个顶点之间的边权的大小ex[u][v]表示两种货币之间的汇率,若要找一个最有利的兑换序列,把货币s兑换成货币t,即在若干种兑换序列中选择 ...

  9. [C]二级指针

    二级指针即“指向指针的指针”: 下面的实例代码创建了一个二级指针c int a = 5; int* b = &a; int** c = &b; 你不能这样 int a = 5; int ...

  10. 如何用Axure快速制作APP交互原型

    对于产品经理来说,熟练使用一些常用软件是一项十分必要的技能.其中,作为一个专业的快速原型设计工具,Axure RP无疑在产品人心中拥有一个难以撼动的地位.但就要PS一样,虽然足够专业,但同样也会存在使 ...