English 中文(简体)
页: 1
原标题:asp.net: where to put code to redirect users without a session to the homepage?

我的网页上装满页数,其中大多数需要一些届会变量才能运行。

i 想在我看来采用一些防御守则。 在什么地方,最适于像:

if (Session.Count == 0){
                Response.Redirect("~/default.aspx");
}

EDIT:如果目前一页已经消失,如何检查。 页: 1

最佳回答

很困难,幸运的是,它得到解决。

页: 1

这里是法典

    /// <summary>
    /// The event occurs just after Initialization of Session, and before Page_Init event
    /// </summary>
    protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
    {
        // here it checks if session is reuired, as
        // .aspx requires session, and session should be available there
        // .jpg, or .css doesn t require session so session will be null
        // as .jpg, or .css are also http request in any case
        // even if you implemented URL Rewritter, or custom IHttp Module
        if (Context.Handler is IRequiresSessionState
               || Context.Handler is IReadOnlySessionState)
        {
            // here is your actual code
            // check if session is new one
            // or any of your logic
            if (Session.IsNewSession
                || Session.Count < 1)
            {
                // for instance your login page is default.aspx
                // it should not be redirected if,
                // if the request is for login page (i.e. default.aspx)
                if (!Context.Request.Url.AbsoluteUri.ToLower().Contains("/default.aspx"))
                {
                    // redirect to your login page
                    Context.Response.Redirect("~/default.aspx");
                }
            }
        }
    }

www.un.org/Depts/DGACM/index_spanish.htm Edit 1: Explanation & Conclusions

其中一个故事讲述了

发生了大量事件。

Actually events in Global.asax raises in the following sequence

  1. Validate Request // looks just internal mechanism
  2. 履行《乌拉圭回合行动计划》

  3. Raise the BeginRequest activity.

  4. Raise the AuthenticateRequest event.
  5. Raise the PostAuthenticateRequest event.
  6. Raise the AuthorizeRequest event.
  7. Raise the PostAuthorizeRequest event.
  8. Raise the ResolveRequestCache event.
  9. Raise the PostResolveRequestCache event.
  10. Just selects a class who implemented IHttpHandler for the application // looks just internal mechanism
  11. Raise the PostMapRequestHandler event.
  12. Raise the AcquireRequestState event. just before raising this event asp.net loads the State like Session
  13. Raise the PostAcquireRequestState event.
  14. Raise the PreRequestHandlerExecute event.
  15. Call the ProcessRequest method

Conclusion: All the events before AcquireRequestState event don t have Session object, because Session is not loaded by ASP.Net, so any event from *"AcquireRequestState** event gives Session object therefore this problem solves. However some checks are required as I mentioned in above code

问题回答

一种方法是在<代码>上设置一个进行这一检查的页基级。 页: 1 另一种方法是将电离层带入。 虽然在<代码>Application_BeginRequest上没有会议,但应当在Application_AcquireRequestState中提供。 由于不是标准网络要求,这应使与会者能够参加本届会议,以完成你想要的东西。

http://www.un.org/Depts/DGACM/index_french.htm

总结我们的想法:

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
    if ((Session.Count == 0) &&
       !(Request.Url.AbsolutePath.EndsWith("default.aspx",
         StringComparison.InvariantCultureIgnoreCase)))
    {
        Response.Redirect("~/default.aspx");
    }
}

Be careful with your approach. I don t think it is a good idea to validate globally if certain Session information exists or not. It can get become very messy, very fast. Only certain pages might require specific Session variables, which differ from other pages. Further down the road you might even have some content which can be safely accessed without any existing Session state. Then you will have to start coding exceptions to your rule...

你在这些会议上储存哪些信息? 如果你进一步阐述,我们或许可以提出更好的办法。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签