在ASP.NET对象持久化(后背上之间) [英] Object persistence (between post-backs) in ASP.NET

查看:151
本文介绍了在ASP.NET对象持久化(后背上之间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发一个相当大的web应用程序的过程。多级访问已通过ASP.NET成员资格和角色提供者定义。个人用户也有配置文件(我已经扩展到包括多种任意信息)。

I am in the process of developing a quite large web app. Multiple levels of access have been defined via ASP.NET Membership and Role providers. Individual users also have profiles (which I have extended to include a variety of arbitrary info).

问题是我怎么坚持后背上的用户?

The question is how do I persist the user between post backs?

我可以使用类似HttpContext.Current存储用户,但它的寿命只有从一后回到另一个。这是我的理解,我将不得不在后回(的OnInit / pageLoad的)的开始检索用户对象,然后将其放回HttpContext的在每个回帖子的结尾。似乎有点效率低下。

I could use something like HttpContext.Current to store the user, but its lifespan is only from one post-back to another. It's my understanding that I would have to retrieve the user object at the beginning of the post-back (OnInit/PageLoad) and then drop it back in the HttpContext at the end of each post-back. Seems kind of inefficient.

(我不知道如果我能做到这一点有一个HttpModule?)

(I wonder if i can do this with an HttpModule?)

此外,即使是ASP.NET它只是一个普通的用户一个静态的currentUser实例。我需要携带整个轮廓(这是我在SQL创建)。

Also, even though there is a static CurrentUser instance in ASP.NET it is simply a generic User. I need to carry the entire profile (which I created in SQL).

要回顾一下,问题是:A)是可以延长由ASP.NET提供的静态的currentUser实例,以便在被通过正常渠道坚持将携带任意信息?如果没有,我怎么能坚持在整个用户会话的持续时间等静态信息(直到用户注销或会话过期/超时事件是由在Global.asax解雇)?

To recap, the question is: a) is it possible to extend the static CurrentUser instance given by ASP.NET so that it would carry arbitrary information while being persisted through normal channels? If No, how can I persist such static information throughout the duration of the User session (until the user either logs off, or the Session Expire/Timeout event is fired by the global.asax)?

任何帮助将大大AP preciated!
谢谢!
bleepzter

Any help will be greatly appreciated! Thanks! bleepzter

推荐答案

使用挂接到PostAuthenticateRequest一个IHttpModule的,那么你的用户存储在HttpContext.Current.User

Use an IHttpModule that hooks into the PostAuthenticateRequest, then store your user in the HttpContext.Current.User

例如:

public class MyModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.PostAuthenticateRequest += PostAuthenticateRequest;
    }

    private void PostAuthenticateRequest(object sender, EventArgs e)
    {
        var app = (HttpApplication)sender;
        var httpContext = app.Context;
        IMyPrincipal principal = null;

        principal = new MyPrincipal(httpContext.User.Identity);

        httpContext.User = principal;
    }
}

这篇关于在ASP.NET对象持久化(后背上之间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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