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. MySQL5.7 锁定用户【转】

    使用ALTER USER 语句锁定 mysql>ALTER USER 'demo'@'localhost' ACCOUNT LOCK; Query OK, rows affected (0.00 ...

  2. HTTP协议02-请求和响应的报文构成

    HTTP协议和TCP/IP协议族内的其他众多协议相同,用于客户端与服务器之间的通信,请求访问文本或图像等资源的一端+称为客户端,而提供资源响应的一端称为服务端. 应用HTTP协议时,请求必定是客户端发 ...

  3. android studio 学习之一 安装和基本使用

    一.简介 Android Studio 是一个Android集成开发工具,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提供了集成的 Android 开 ...

  4. 训练报告 (2014-2015) 2014, Samara SAU ACM ICPC Quarterfinal Qualification Contest

    Solved A Gym 100488A Yet Another Goat in the Garden   B Gym 100488B Impossible to Guess Solved C Gym ...

  5. git下载指定的版本

    1.查看提交历史    sudo git log 打印如下内容: commit 2e3c19d412ab6a99bb51f338f71537a720a9c706   Author: huangbaog ...

  6. Keil和SourceInsight中文乱码解决方法

    一.KEIL乱码 到菜单栏Edit--->Configuration-->Encoding ---ChineseSimplied 二.SourceInsight乱码 错误现象:注释乱码,查 ...

  7. 使用Filezilla搭建FTP服务器

    1.FTP over TLS is not enabled, users cannot securely http://blog.sina.com.cn/s/blog_4cd978f90102vtwl ...

  8. 【原创】大数据基础之Logstash(1)简介、安装、使用

    Logstash 6.6.2 官方:https://www.elastic.co/products/logstash 一 简介 Centralize, Transform & Stash Yo ...

  9. Java实现三大简单排序算法

    一.选择排序 public static void main(String[] args) { int[] nums = {1,2,8,4,6,7,3,6,4,9}; for (int i=0; i& ...

  10. linux强制将数据写入磁盘,防止丢失内存的数据

    sync命令文件系统管理 sync命令用于强制被改变的内容立刻写入磁盘,更新超块信息. 在Linux/Unix系统中,在文件或数据处理过程中一般先放到内存缓冲区中,等到适当的时候再写入磁盘, 以提高系 ...