【译】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这个博主. ...
随机推荐
- 【模板】AC自动机(加强版)
题目描述 有个由小写字母组成的模式串以及一个文本串.每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串中出现的次数最多. 输入输出格式 输入格式: 输入含多组数据. 每组数据的第一行为一 ...
- 利用jdbc简单封装一个小框架(类似DBUtils)
利用jdbc写的一个类似DBUtils的框架 package com.jdbc.orm.dbutils; import java.io.IOException; import java.io.Inpu ...
- webpack——devtool里的7种SourceMap模式
我们先来看看文档对这 7 种模式的解释: 模式 解释 eval 每个module会封装到 eval 里包裹起来执行,并且会在末尾追加注释 //@ sourceURL. source-map 生成一个S ...
- Android 性能测试之方向与框架篇
假期结束,你的状态有没有回归?那么,放空脑袋后,先来学习学习,欢迎大家继续关注腾讯云技术社区. 作者:李帅 导语 借项目的开发周期,把思考了一段时间的场景化性能测试框架搭建起来,包括 耗电性能测试.内 ...
- 【Vue】浅谈Vue(一):从模板语法数据绑定、指令到计算属性
写在前面 今年前端届比较有意思,从大漠穷秋发表文章比较angular和vue,继而致歉vue作者.社区,从谷歌辞去Angular Developer PM in China一职并且呼吁大家停止各种无谓 ...
- pseudocode of zigzag conversion
1.Title : The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...
- nodejs+mongoose操作mongodb副本集实例
继上一篇设置mongodb副本集之后,开始使用nodejs访问mongodb副本集: 1:创建项目 express 项目名称 2:npm install mongoose 安装mongo ...
- vb6.0的各种SHELL,CMD内部命令、外部命令、SHELL任意文件
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (B ...
- JS中的类型识别
JS为弱类型语言,所以类型识别对JS而言尤为重要,JS中常用的类型识别方法有4种:typeof.Object.prototype.toString.constructor和instanceof. (1 ...
- UVa1635 - Irrelevant Elements
通过观察发现其规律符合杨辉三角 需要注意的是最后ai的系数是C(i-1,n-1) 那么,问题就可以变成判断C(0,n-1),C(1,n-1)....C(n-1,n-1)哪些是m的倍数 只需要计算出m的 ...