【译】Asp.Net Identity Cookies 格式化-中英对照版
原文出处
Trailmax Tech
Max Vasilyev: ASP.Net MVC development in Aberdeen, Scotland
I’ve been reached out by one of my readers with a list of questions about AspNet Identity. First I thought the questions will be easy and I’ll wing them, but turned out that every single question in the list is worth of a separate blog post. So I’ll do a mini-series of Q-A posts about Identity framework and related stuff.
我的读者联系到我,并向我提出了一系列关于 AspNet Identity的问题。一开始我觉得这些问题都会比较简单,我能够轻松的回答他,但结果表明,这里面的每一个问题都值得写一篇单独的文章。所以我会写一个Mini系列关于Identity框架的“Q-A(问与答)”博客。
First question is about security token in a Cookie and how it is computed: Identity Framework creates a token in the cookie, token containing the same information (User.Id, Security Stamp, etc.); if the same user loggen-in in Chrome and Firefox, why are the cookies different?
第一个问题是关于Cookie安全Token以及它是如何被计算(computed)出来的:如果同一个用户在 Chrome 和Firefox中登陆,那么Identity Framework 在Cookie中创建token,token包含相同的信息(用户Id、 Security Stamp、等等),但是为甚么这两个Cookie是不同的?
I did think about it for a while, I guessed that cookie creation and expiration is encoded in the cookie, along with some sort of signature of the cookie. My guess was not far from the real world.
我思考了一下,我猜 cookie创建信息和过期信息被编码在了cookie中,可能还有cookie的某种签名信息也被一同保存了。我觉得我猜的很接近答案。
Fortunately, the code that deals with cookies is in Katana Project and is open-sourced.
幸运的是,处理cookie的代码就在开源的 Katana Project 中。
Here is the pipeline how cookie is created on user sign-in:
下面就是用户登录是cookie的创建过程
- Identity framework creates ClaimsPrincipal object from the record in your database (that is represented by ApplicationUser class)
Identity framework根据你数据库中的信息创建ClaimsPrincipal对象(它对应于ApplicationUser类) - Identity adds list of default claims like ApplicationUser.Id and SecurityStamp to the claims on the principal.
Identity向principal中添加一些列默认claim,例如ApplicationUser.Id和SecurityStamp - ClaimsPrincipal is passed to OWIN middleware: CookieAuthenticationHandler
ClaimsPrincipal被传递到CookieAuthenticationHandler这个OWIN中间件中 - Authentication Handler is doing a lot of checking for different things, like should the cookie be marked as secure or persistent, sets the expiry date and many other things. Result of these checks is AuthenticationTicket which has ClaimsIdentity with list of claims and AuthenticationProperties which is a dictionary with load of data like cookie issue date, expiry date, is cookie persistent, etc.
Authentication Handler对不同的东西都做了很多检查,比如,检查每个cookie是否应该被标记为secure或者persistent,设置过期时间等等很多事情。这一系列检查(checks,含有操作的意思)的结果就是生成一个AuthenticationTicket,这个AuthenticationTicket拥有一个含有一系列claim和AuthenticationProperties(认证属性)的ClaimsIdentity,AuthenticationProperties是一个包含很多数据的字典(dictionary),里面的数据可能是 cookie issue 日期,过期日期,是否持久保存cookie,等等 - Auth ticket and claims principal is then passed down to SecureDataFormat class where data is serialised, encrypted and Base64 encoded.
认证票据和claims principal接下来被传递到SecureDataFormat类中,数据将在这个类中序列化、加密、Base64编码
The last step contains a lot of movements, let’s look deeper into it.
最后一步包含很多动作,我们再深入些。
Authentication ticket is serialised via TicketSerializer. There ClaimsIdentity is written into memory stream: some properties and list of the claims. Then AuthenticationProperties (with cookie set, expiry dates) is also written to memory stream. Then this memory stream is GZipped and returned for further processing.
认证票据由TicketSerializer进行序列化。这里ClaimsIdentity被写入内存流:包含一些属性和一个claim列表。然后AuthenticationProperties(包括cookie 设置,过期日期)也被写入了内存流。然后内存流被GZip压缩,以备接下来的处理。
Next step in the pipeline is encryption. Encryption is borrowed from .Net class DpapiDataProtector. You can read documentation on MSDN. I’m not sure about the strength of the encryption. The documentation says that purpose parameters are effectively a list of passwords. And now here I’ve seen in OWIN where you can set your own encryption password and I can see that the main password is set to “Microsoft.Owin.Security.IDataProtector”. So my guess would be that no secure information should go into cookie, i.e. don’t put your connection strings into user claims!
管道中的下一步就是加密。加密操作由 .Net 类库中的DpapiDataProtector提供。你可以在MSDN上阅读相关文档。我不确定加密的强度。文档中说目标参数事实上是一个密码列表。现在我在OWIN中看到了一个地方你可以在这里设置你自己的加密密码,而且我看到这里主密码是“Microsoft.Owin.Security.IDataProtector”。所以我的猜测是安全的信息不应该被放进cookie里,比如:别把连接字符串放在claim里!(注:这里的翻译可能不准确,建议此段参考阅读原文)
After the encryption, stream of bytes is Base64-encoded to be transferable over the wire. And finally prepared to be set as a cookie header on HTTP Response.
在加密之后,字节流使用Base64编码以易于网络传输。最后准备好以等待被设置到Http响应的cookie头上。
Overall the process will look like this great drawing:
所有的过程如下图所示

Anyway, the answer to the original question why the cookies are different in different browsers would be as follows: because part of the signature contains cookie creation and expiry dates, these would be different in different browsers. So data encrypted in cookie turns out to be different, the value of the cookie will be different.
反正,原来的问题:“为什么在不同浏览器中的cookies是不同的?”的答案是:因为签名中包含cookie的创建信息和过期时间,这在不同浏览器中是不同的,所以cookie中的加密信息也是不同的,所以cookie也是不同的。
【译】Asp.Net Identity Cookies 格式化-中英对照版的更多相关文章
- 【译】Asp.Net Identity Cookies 格式化
原文出处 Trailmax Tech Max Vasilyev: ASP.Net MVC development in Aberdeen, Scotland 中英对照版 我的读者联系到我,并向我提出了 ...
- PCMM(人力资源能力成熟度模型)V2.0中英对照版发布
PCMM中英版终于发布 时光荏苒,从当初的回眸到如今的回头,这才发现:坚守一份承诺是多么的不易! 一年多了,这份承载殷切期待的作品--<PCMM(人力资源能力成熟度模型)V2.0 (中英文对照版 ...
- orchard 中文文档 中英对照版
ORCHARD CMS a free, open source, community-focused Content Management System built on the ASP.NET MV ...
- TLCL中英对照版
TLCL中英文对照阅读网址:http://billie66.github.io/TLCL/book/index.html 感谢好奇猫团队(http://haoqicat.com/about/team) ...
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...
- eclipse菜单解释及中英对照
在使用Eclipse作为开发工具的时候,建议使用英文版本的(直接百度从官网下就行,这里不详细描述,如果有问题,咱们私聊).虽然中文版本的对于和我一样对英文是小白的看起来特别爽,但是公司大多是英文版本的 ...
- [转]从普通DLL中导出C++类 – dllexport和dllimport的使用方法(中英对照、附注解)
这几天写几个小程序练手,在准备将一个类导出时,发现还真不知道如果不用MFC的扩展DLL,是怎么导出的.但我知道dllexport可以导出函数和变量,而且MFC扩展DLL就算是使用了MFC的功能,但 ...
- webstorm快捷键 webstorm keymap内置快捷键英文翻译、中英对照说明
20160114参考网络上的快捷键,整理自己常用的: 查找/代替shift+shift 快速搜索所有文件,简便ctrl+shift+N 通过文件名快速查找工程内的文件(必记)ctrl+shift+al ...
- eclipse菜单解释及中英对照《二》
上篇文章主要介绍了eclipse中每个大的标题下的中英文及其用法. 感谢http://blog.csdn.net/li_jinjian2005/article/details/2831641这个博主. ...
随机推荐
- 一个“.java”文件中是否可以包含多个类(不是内部类)?有什么限制?
可以,若这个类的修饰符是public则,其类名须与文件名相同.
- Deep learning:一(基础知识_1)
本文纯转载: 主要是想系统的跟tornadomeet的顺序走一遍deeplearning; 前言: 最近打算稍微系统的学习下deep learing的一些理论知识,打算采用Andrew Ng的网页教程 ...
- Ubuntu安装opencv3.x系列
p { margin-bottom: 0.25cm; direction: ltr; color: rgb(0, 0, 0); line-height: 120% } p.western { font ...
- 安装CentOS7精简版后的配置工作
CentOS7完整版有7.7G,太大了下载起来比较费劲,还是下载了精简版,但是精简版安装以后很多命令都没有,还要动手配yum源,按需安装 国内的yum源比较好的就是163的了,配置方法: 1,进入yu ...
- Hive导出复杂数据到csv文件
工作中经常遇到使用Hive导出数据到文本文件供数据分析时使用.Hive导出复杂数据到csv等文本文件时,有时会遇到以下几个问题: 导出的数据只有数据没有列名. 导出的数据比较复杂时,如字符串内包含一些 ...
- 由 System.arraycopy 引发的巩固:对象引用 与 对象 的区别
作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...
- 读Zepto源码之fx_methods模块
fx 模块提供了 animate 动画方法,fx_methods 利用 animate 方法,提供一些常用的动画方法.所以 fx_methods 模块依赖于 fx 模块,在引入 fx_methods ...
- Java面向对象 IO (一)
Java面向对象 IO (一) 知识概要: (1)IO概述 (2)IO流的常用基类 (3)IO程序的书写 (4)字符流 写入 读取 文本文件的两种读取方式 ...
- Jquery实现数据双向绑定(赋值和取值),类似AngularJS
<!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...
- js 将一大段时间均分为很多个小时间段
最近写项目,遇到一个将选中时间段平均分割为若干小段,然后根据小段时间在数据库查询求均值的问题,后台大哥犯懒,非说后台做不了,让我分好传给他ヾ(. ̄□ ̄)ツ゜゜゜好气呦,但还要保持微笑,我就是这么懂礼貌 ...