【发布时间】:2023-04-06 12:55:01
【问题描述】:
从 HttpContext.Current.Request.Cookies 中读取 HttpContext.Current.User 的时间和地点?
我已经开始查看 ASP.NET MVC 5 源代码 http://aspnetwebstack.codeplex.com/,但无法弄清楚主体的第一次设置位置。
【问题讨论】:
标签: asp.net-mvc authentication cookies
从 HttpContext.Current.Request.Cookies 中读取 HttpContext.Current.User 的时间和地点?
我已经开始查看 ASP.NET MVC 5 源代码 http://aspnetwebstack.codeplex.com/,但无法弄清楚主体的第一次设置位置。
【问题讨论】:
标签: asp.net-mvc authentication cookies
如果您使用联合身份验证,则 SessionAuthenticationModule 使用其配置的 CookieHandler 将 cookie 反序列化为 SessionSecurityToken。然后它使用令牌为用户创建一个ClaimsPrincipal。这个ClaimsPrincipal 然后用于设置Thread.CurrentPrincipal 和HttpContext.User 属性。此过程发生在 ASP.NET 管道的AuthenticateRequest 和PostAuthenticateRequest 步骤中。由于它是在 ASP.NET 管道中完成的,我怀疑你会在 MVC 源代码中找到它。
您可以在MSDN 上找到有关SessionAuthenticationModule 的更多信息。
This is a great post 在 FormsAuth 和 FedAuth 上。它解释了所有位如何在 ASP.NET 管道中组合在一起。
【讨论】:
我找错地方了。身份验证现在使用 OWIN 中间件。 This blog is useful。源代码位于一个名为 katana 的项目中。 The source code is here。还有here is the official asp.net blog post
简而言之。 Owins 是处理 http 请求的新方法。 owins 请求通过处理请求的“中间件”管道传递。 katana 项目(上图)中的 CookieAuthenticationMiddleware 负责解码身份验证 cookie。
【讨论】: