原文出处

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的创建过程

  1. Identity framework creates ClaimsPrincipal object from the record in your database (that is represented by ApplicationUser class)
    Identity framework根据你数据库中的信息创建ClaimsPrincipal对象(它对应于 ApplicationUser类)
  2. Identity adds list of default claims like ApplicationUser.Id and SecurityStamp to the claims on the principal.
    Identity向principal中添加一些列默认claim,例如ApplicationUser.Id和SecurityStamp
  3. ClaimsPrincipal is passed to OWIN middleware: CookieAuthenticationHandler
    ClaimsPrincipal被传递到CookieAuthenticationHandler这个OWIN中间件中
  4. 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,等等
  5. 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 格式化-中英对照版的更多相关文章

  1. 【译】Asp.Net Identity Cookies 格式化

    原文出处 Trailmax Tech Max Vasilyev: ASP.Net MVC development in Aberdeen, Scotland 中英对照版 我的读者联系到我,并向我提出了 ...

  2. PCMM(人力资源能力成熟度模型)V2.0中英对照版发布

    PCMM中英版终于发布 时光荏苒,从当初的回眸到如今的回头,这才发现:坚守一份承诺是多么的不易! 一年多了,这份承载殷切期待的作品--<PCMM(人力资源能力成熟度模型)V2.0 (中英文对照版 ...

  3. orchard 中文文档 中英对照版

    ORCHARD CMS a free, open source, community-focused Content Management System built on the ASP.NET MV ...

  4. TLCL中英对照版

    TLCL中英文对照阅读网址:http://billie66.github.io/TLCL/book/index.html 感谢好奇猫团队(http://haoqicat.com/about/team) ...

  5. 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 重点: 实现多级子目录的压缩, ...

  6. eclipse菜单解释及中英对照

    在使用Eclipse作为开发工具的时候,建议使用英文版本的(直接百度从官网下就行,这里不详细描述,如果有问题,咱们私聊).虽然中文版本的对于和我一样对英文是小白的看起来特别爽,但是公司大多是英文版本的 ...

  7. [转]从普通DLL中导出C++类 – dllexport和dllimport的使用方法(中英对照、附注解)

      这几天写几个小程序练手,在准备将一个类导出时,发现还真不知道如果不用MFC的扩展DLL,是怎么导出的.但我知道dllexport可以导出函数和变量,而且MFC扩展DLL就算是使用了MFC的功能,但 ...

  8. webstorm快捷键 webstorm keymap内置快捷键英文翻译、中英对照说明

    20160114参考网络上的快捷键,整理自己常用的: 查找/代替shift+shift 快速搜索所有文件,简便ctrl+shift+N 通过文件名快速查找工程内的文件(必记)ctrl+shift+al ...

  9. eclipse菜单解释及中英对照《二》

    上篇文章主要介绍了eclipse中每个大的标题下的中英文及其用法. 感谢http://blog.csdn.net/li_jinjian2005/article/details/2831641这个博主. ...

随机推荐

  1. 无向图广度优先遍历及其matlab实现

    广度优先遍历(breadth-first traverse,bfts),称作广度优先搜索(breath first search)是连通图的一种遍历策略.之所以称作广度优先遍历是因为他的思想是从一个顶 ...

  2. jQuery和js获取select元素的选中项value?

    1.jQuery方式获取:$("#test").val(); 2.js方式获取:document.getElementById("test").value;

  3. Python单元测试框架

    目录 概况 系统要求 使用PyUnit构建自己的测试 安装 测试用例介绍 创建一个简单测试用例 复用设置代码:创建固件 包含多个测试方法的测试用例类 将测试用例聚合成测试套件 嵌套测试用例 测试代码的 ...

  4. 基于java的后台截图功能的实现

    Java后台截图功能的实现 背景介绍: 在近期开发的可视化二期项目中的邮件项目中,邮件中的正文中含有图片.该图片的产生是将一些html网页转为图片格式,刚开始考虑使用第三方组件库html2image和 ...

  5. JAVA提高三:反射总结

    为前期学习过反射,再这里再次复习总结下:[转载请说明来源:http://www.cnblogs.com/pony1223/p/7659210.html ] 一.透彻分析反射的基础_Class类 Cla ...

  6. Kindeditor JS 取值问题以及上传图片后回调等

    KindEditor.ready(function (K) { var editor = K.create('#editor_id', { //上传管理 uploadJson: '/js/kinded ...

  7. 学习PID

    最近在想自己的文章有些是不是写的太难以理解了呢.........竟然好多人看了还是会直接问我很多问题....... 其实PID哈靠自己想像就能自己写出来自己的代码,也许是网上的讲的太过的高深什么积分微 ...

  8. Sqoop1.99.7将MySQL数据导入到HDFS中

    准备 本示例将实现从MySQL数据库中将数据导入到HDFS中 参考文档: http://sqoop.apache.org/docs/1.99.7/user/Sqoop5MinutesDemo.html ...

  9. Java可变参数以及一个简单应用

    可变参数: Java1.5增加了新特性:可变参数:适用于参数个数不确定,类型确定的情况,java把可变参数当做数组处理. 注意:可变参数必须位于最后一项. 原因:当可变参数个数多余一个时,必将有一个不 ...

  10. CoreCLR源码探索(七) JIT的工作原理(入门篇)

    很多C#的初学者都会有这么一个疑问, .Net程序代码是如何被机器加载执行的? 最简单的解答是, C#会通过编译器(CodeDom, Roslyn)编译成IL代码, 然后CLR(.Net Framew ...