当我最早可以在ASP.NET MVC页面生命周期访问会话? [英] When is the earliest I can access SESSION in the ASP.NET MVC page lifecycle?

查看:109
本文介绍了当我最早可以在ASP.NET MVC页面生命周期访问会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是基本相同的问题<一href=\"http://stackoverflow.com/questions/765054/whens-the-earliest-i-can-access-some-session-data-in-global-asax\">765054在计算器上。我只问一次,因为接受的答案是不正确(无法访问的Application_BeginRequest会话对象)。

My question is essentially the same as question 765054 on StackOverflow. I'm only asking it again because the accepted answer is incorrect (you can not access the session object in Application_BeginRequest).

我们的使用情况是,我们要通过身份验证的用户的用户对象存储在会话中。因此,在后续请求中,我们可以正确地在基于会话的用户对象上设置的IPrincipal和IIdentity的。

Our use case is that we want to store the authenticated user's user object in the session. So in subsequent requests, we can correctly set the IPrincipal and IIdentity based on the user object in session.

推荐答案

我使用的是基本控制器和压倒一切的OnActionExecuting类似的事情。无论这是可以做到的最早与否,我不知道,但我知道,在执行前,你的动作(因此前视图渲染)就会出现这种情况。另外,您可能希望有一个自定义的授权的属性,你想要做什么。这可能是要确保它的完成其他属性之前运行的唯一途径。

I do similar things using a base controller and overriding OnActionExecuting. Whether this is the earliest it can be done or not, I don't know, but I do know that it will happen before your action is executed (and thus before the view is rendered). Alternatively you might want to have a custom authorize attribute that does what you want. This might be the only way to make sure that it's done prior to other attributes running.

这篇关于当我最早可以在ASP.NET MVC页面生命周期访问会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆