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. 测试cpu的简单工具-dhrystone【转】

    转自:https://blog.csdn.net/feixiaoxing/article/details/9005587 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog ...

  2. windows 自带winmm.dll播放音频问题

    同事用的一个录音小程序在他机器上可以用,换了两个电脑不能用,获取音频长度时总是0,检查代码也没有发现具体问题.最后发现是电脑声卡驱动的问题.更新声卡驱动好了. 附上播放音频的代码: 首先,导入dll文 ...

  3. 使用openssl命令制作ecc证书

    # openssl ecparam -out EccCA.key -name prime256v1 -genkey # openssl req -config openssl.cnf -key Ecc ...

  4. js 常用的工具函数

    1 类型判断 isString (o) { //是否字符串 return Object.prototype.toString.call(o).slice(8, -1) === 'String' } i ...

  5. MySQL索引操作

    创建普通索引 CREATE INDEX index_name ON table_name(column1,column2); 另一种建立方式: ALTER TABLE table_name ADD I ...

  6. FastCGI sent in stderr: "PHP Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '&lt;' not found in

    2018-4-16 17:59:03 星期一 1. 发送带有xml参数的请求时如果是用php curl, 需要将数组形式的post data 用 http_build_query() 转换 2. 接收 ...

  7. MySQL数据库char与varchar的区别分析及使用建议

    在数据库中,字符 型的数据是最多的,可以占到整个数据库的80%以上.为此正确处理字符型的数据,对于提高数据库的性能有很大的作用.在字符型数据中,用的最多的就是 Char与Varchar两种类型.前面的 ...

  8. interface{} 泛型编程

    转自: 张晓龙 中兴开发者社区 https://mp.weixin.qq.com/s/EEUtTykcrXhcM2hJT01SoQ 序言 众所周知,Golang中不支持类似C++/Java中的标记式泛 ...

  9. Select查询命令

    一开始SELECT查询的命令为     SELECT * FROM employee;     SELECT 要查询的列名 FROM 表名字 WHERE 限制条件;     若要查询所有内容,就用*代 ...

  10. 部署apache-tomcat环境

    软件体系: C/S:客户端/服务器,例如qq等app都属于C/S体系,除了编写服务端代码还需要编写客户端 优点:展现比较好,客户端会承受一点运算压力,安全性比较好 缺点:更新服务端的同时还需要更新客户 ...