原文出处

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. 01背包java实现(入门到精通)

    一.什么是01背包 01背包是在M件物品取出若干件放在空间为W的背包里,每件物品的体积为W1,W2至Wn,与之相对应的价值为P1,P2至Pn.01背包是背包问题中最简单的问题.01背包的约束条件是给定 ...

  2. Temperature hdu 3477

    Temperature Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. Asp数据转Json

    需要引用的文件: json.asp(可在JSON官网下载,也可在底部链接的demo中直接拷贝该文件) Conn.asp是链接数据库文件 <%@LANGUAGE="%> <% ...

  4. [js高手之路] javascript面向对象写法与应用

    一.什么是对象? 对象是n个属性和方法组成的集合,如js内置的document, Date, Regexp, Math等等 document就是有很多的属性和方法, 如:getElementById, ...

  5. Centos7虚拟机桥接模式

    主机ping不通虚拟机centos7系统的ip大多有以下原因: 1.主机与centos7ip段对应 (关键是网关.dns服务器要一一对应,具体操作见后文) 2.对应后仍然不通的,可关闭主机与cenos ...

  6. CoreData归纳使用

    1.CoreData简介 2.CoreData数据模型 3.CoreData的主要对象 4.使用CoreData实现数据存储 一.CoreData简介 CoreData用做数据持久化,是数据持久化的最 ...

  7. win10 uwp 自定义控件初始化

    我遇到一个问题,我在 xaml 用了我的自定义控件,但是我给他设置了一个值,但是什么时候我才可以获得这个值? 本文告诉大家,从构造函数.loaded.Initialized 的调用过程. 用最简单的方 ...

  8. Windows NT 之父 - David Cutler

    David Cutler,大卫·卡特勒,一位传奇程序员,1988年去微软前号称硅谷最牛的内核开发人员,是VMS和Windows NT的首席设计师,被人们成为“操作系统天神”.他曾供职于杜邦.DEC等公 ...

  9. Java基础总结--面向对象1

    ---面向对象的概念---1.面向过程与面向对象的区别面向过程:是一种思维习惯,解决一个问题的时候靠的是一个个方法调用--核心是动作面向对象:是一种更接近生活中解决问题的思维习惯,解决特定的问题靠对象 ...

  10. 数据帧CRC32校验算法实现

    本文设计思想采用明德扬至简设计法.由于本人项目需要进行光纤数据传输,为了保证通信质量要对数据进行校验.在校验算法中,最简单最成熟的非CRC校验莫属了. 得出一个数的CRC校验码还是比较简单的: 选定一 ...