MVC5在哪里从Cookie读取主体?

时间:2013-11-13 04:02:35

标签: asp.net-mvc authentication cookies

从HttpContext.Current.Request.Cookies读取HttpContext.Current.User的位置和时间?

我已经开始查看ASP.NET MVC 5源代码http://aspnetwebstack.codeplex.com/,但无法确定首次设置主体的位置。

2 个答案:

答案 0 :(得分:2)

如果您使用的是联合身份验证,SessionAuthenticationModule使用其已配置的CookieHandler将Cookie反序列化为SessionSecurityToken。然后,它使用令牌为用户创建ClaimsPrincipal。然后,此ClaimsPrincipal用于设置Thread.CurrentPrincipalHttpContext.User属性。此过程发生在ASP.NET管道的AuthenticateRequestPostAuthenticateRequest步骤中。由于它是在ASP.NET管道中完成的,我怀疑你会在MVC源代码中找到它。

您可以在MSDN上找到有关SessionAuthenticationModule的更多信息。

FormsAuth和FedAuth上的

This is a great post。它解释了ASP.NET管道中所有位是如何组合在一起的。

答案 1 :(得分:0)

我正在寻找错误的地方。身份验证现在使用OWIN中间件。 This blog is useful。源代码位于名为katana的项目中。 The source code is here。并here is the official asp.net blog post

简而言之。 Owins是处理http请求的新方法。 owins请求传递给处理请求的'middle ware'管道。来自项目katana(上面)的CookieAuthenticationMiddleware负责解码身份验证cookie。