0

I used forms authentication. In LdapAuthentication.cs I have property

public static string ReturnProject
    {
        get
        {
            return HttpContext.Current.Session["Project"].ToString();
        }
    }

In global.asax.cs I trying to get Session["Project"] from LdapAuthentication.cs for check and ridirect to other pages according with rusult in Session["Project"], but I've got System.NullReferenceException. I cheked Session["Project"] in LdapAuthentication.cs - is ok

protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        if (Request.AppRelativeCurrentExecutionFilePath == "~/")
        {
            if (LdapAuthentication.ReturnProject == "Team Leader")
                HttpContext.Current.RewritePath("~/TLPage.aspx");
            else
                if (LdapAuthentication.ReturnName == "ccobserver")
                    HttpContext.Current.RewritePath("~/ScheduleReport.aspx");
                else
                    HttpContext.Current.RewritePath("~/PersonPage.aspx");
        }
    }

doesn't matter which handler use Application_AcquireRequestState or Application_AuthenticateRequest.

Thanks!

Andrey Saw
  • 171
  • 2
  • 7
  • I doubt Session exists for that event handler. What are you trying to do? I suspect this is an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), you should describe what you really want to have happen. Do you just want to redirect someone after logging in? – mason Mar 12 '15 at 15:51
  • Plus, you need to check that `Session["Project"]` isn't null before you `ToString()` it. – MikeSmithDev Mar 12 '15 at 16:44
  • I trying to get Session["Project"] from LdapAuthentication.cs for check and ridirect to other pages according with rusult in Session["Project"], but I've got System.NullReferenceException. I cheked Session["Project"] in LdapAuthentication.cs - is ok – Andrey Saw Mar 13 '15 at 08:12
  • possible duplicate of [Asp.net System.Web.HttpContext.Current.Session null in global.asax](http://stackoverflow.com/questions/293276/asp-net-system-web-httpcontext-current-session-null-in-global-asax) – Luaan Mar 13 '15 at 08:35
  • Try applying this test: if (System.Web.HttpContext.Current.Session != null) because there it can be called more then once and the session can be null in some case. This is how it works for me. – Syed Mohamed Jun 27 '16 at 10:13

1 Answers1

0

You declared ReturnProject static property, while HttpContext is an instance property of HttpApplication class, implemented in Global.asax.

Static property has no access to instance properties, so removing static modifier from ReturnProject should fix the problem.

EDIT

Now I get it, ReturnProject property declared in LdapAuthentication.cs, not in Global.asax.

Let me explain a bit. Every time request hits a server, new instance of HttpApplication (hence, HttpContext instance property) is created. This way you get access to Request and Session — they are bound to concrete request associated with concrete user. Related answer: “HttpContext.Current.Session” vs Global.asax “this.Session”

What you seem trying to achieve is to pass some session variable (Project) to the LdapAuthentication class in a nice way. There are more than one way to do this, but the obvious way without looking to the source of LdapAuthentication class is to add LdapAuthentication instance field to the Global.asax, passing session variable through the constructor or property initializer. Something like this:

LdapAuthentication.cs

/// <summary>
/// Creates instance of LdapAuthentication with Project value initialized
/// </summary>
public LdapAuthentication(string project) {
    this.ReturnProject = project;
}

Global.asax

private LdapAuthentication auth = new LdapAuthentication(
       HttpContext.Current.Session["Project"].ToString()
);
...
protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        if (Request.AppRelativeCurrentExecutionFilePath == "~/")
        {
            if (auth.ReturnProject == "Team Leader")
                HttpContext.Current.RewritePath("~/TLPage.aspx");
            else
                if (auth.ReturnName == "ccobserver")
                    HttpContext.Current.RewritePath("~/ScheduleReport.aspx");
                else
                    HttpContext.Current.RewritePath("~/PersonPage.aspx");
        }
    }
Community
  • 1
  • 1
Artem Koshelev
  • 10,548
  • 4
  • 36
  • 68
  • thanks, but if I removing static modifier from property, I can get acces to this property in LdapAuthentication.cs from Global.asax : Error An object reference is required for the non-static field, method, or property – Andrey Saw Mar 13 '15 at 09:46
  • Thanks, similar error System.NullReferenceException for private LdapAuthentication auth = new LdapAuthentication( HttpContext.Current.Session["Project"].ToString() ); – Andrey Saw Mar 13 '15 at 11:24