locked
.loggedIn for Session State RRS feed

  • Question

  • User-2073497821 posted

    hi All,

    I just recently began to convert some VB.Net classes over to C# and had this small one which is giving me a huge hassel, I would appreciate any help offered. Im getting object does nto contain a definition for loggedIn and no extension method loggedIn accepting a first argument of type object could not be found (are you missing a using directive or an assembly reference?)

    using System.Web;
    
    namespace ConversionCSharp
    {
        public class AuthenticatedUser : System.Web.UI.Page
        {
            protected void Page_Load()
            {  
             if (!(Session["User"] == null) && (Session["User"].loggedIn))//Error here at .loggedIn - using statement
                {
                    string Here = HttpContext.Current.Request.Url.Host;
                    string GoToValue = "http://" + Here + "/";
                    if (!(Request.QueryString["URL"] == null))
                    {
                        if ((Request.QueryString["URL"].ToString() != ""))
                        {
                            GoToValue = Request.QueryString["URL"].ToString();
                        }
                    }
                    Response.Redirect(GoToValue);
                }
            }
        }
    }



    Monday, April 1, 2013 3:33 PM

Answers

  • User281315223 posted

    This is because the object that is stored within the Session is in fact just a a generic object, which no specific properties (so the LoggedIn property wouldn't exist on it)

    After checking that it wasn't null, you should attempt to cast your Session["User"] as whichever type of class has the LoggedIn Property (or if you wanted to check if an actual user is authorized and logged in you may want to use the Page.User.Identity.IsAuthenticated property) : 

    protected void Page_Load()
    {
          if (Session["User"] != null)
          {
                //You could cast the Session variable to whatever type it was set as originally here
                YourUserClass user = Session["User"] as YourUserClass;
                //You will then be able to access your loggedIn property through user.loggedIn
    
                string Here = HttpContext.Current.Request.Url.Host;
                string GoToValue = "http://" + Here + "/";
                if (Request.QueryString["URL"] != null)
                {
                      if (Request.QueryString["URL"].ToString() != "")
                      {
                            GoToValue = Request.QueryString["URL"].ToString();
                      }
                }
                Response.Redirect(GoToValue);
          }
    }

    or using the IsAuthenticated method : 

    protected void Page_Load()
    {
          if (Session["User"] != null && Page.User.Identity.IsAuthenticated)
          {
                string Here = HttpContext.Current.Request.Url.Host;
                string GoToValue = "http://" + Here + "/";
                if (Request.QueryString["URL"] != null)
                {
                      if (Request.QueryString["URL"].ToString() != "")
                      {
                            GoToValue = Request.QueryString["URL"].ToString();
                      }
                }
                Response.Redirect(GoToValue);
          }
    }

    The above examples are very basic and more information would really be necessary to give you any kind of specific answer.

    Could you post the code where you are actually setting the "User" Session variable?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 1, 2013 3:54 PM
  • User-2073497821 posted

    Hi Rion,

    Ill have to look around for that, I dont think Ive gotten to that class yet. Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 1, 2013 4:00 PM

All replies

  • User-1360095595 posted

    Session ["User"] returns an object which doesn't have a loggedin method.

    Monday, April 1, 2013 3:42 PM
  • User-2073497821 posted

    In the VB class it does, however after ["User"] dot, I get Equals, GetHashCode, GetType and ToString.

    I tried HttpContext.Current....but could not find a logged in method and my experience with VB is zero other than these conversion. What suggestions might you have? 

    Thanks

    Monday, April 1, 2013 3:50 PM
  • User-1360095595 posted

    You need to cast the session["user"] to whatever type was stored in it in the first place. 

    Monday, April 1, 2013 3:52 PM
  • User281315223 posted

    This is because the object that is stored within the Session is in fact just a a generic object, which no specific properties (so the LoggedIn property wouldn't exist on it)

    After checking that it wasn't null, you should attempt to cast your Session["User"] as whichever type of class has the LoggedIn Property (or if you wanted to check if an actual user is authorized and logged in you may want to use the Page.User.Identity.IsAuthenticated property) : 

    protected void Page_Load()
    {
          if (Session["User"] != null)
          {
                //You could cast the Session variable to whatever type it was set as originally here
                YourUserClass user = Session["User"] as YourUserClass;
                //You will then be able to access your loggedIn property through user.loggedIn
    
                string Here = HttpContext.Current.Request.Url.Host;
                string GoToValue = "http://" + Here + "/";
                if (Request.QueryString["URL"] != null)
                {
                      if (Request.QueryString["URL"].ToString() != "")
                      {
                            GoToValue = Request.QueryString["URL"].ToString();
                      }
                }
                Response.Redirect(GoToValue);
          }
    }

    or using the IsAuthenticated method : 

    protected void Page_Load()
    {
          if (Session["User"] != null && Page.User.Identity.IsAuthenticated)
          {
                string Here = HttpContext.Current.Request.Url.Host;
                string GoToValue = "http://" + Here + "/";
                if (Request.QueryString["URL"] != null)
                {
                      if (Request.QueryString["URL"].ToString() != "")
                      {
                            GoToValue = Request.QueryString["URL"].ToString();
                      }
                }
                Response.Redirect(GoToValue);
          }
    }

    The above examples are very basic and more information would really be necessary to give you any kind of specific answer.

    Could you post the code where you are actually setting the "User" Session variable?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 1, 2013 3:54 PM
  • User-2073497821 posted

    Hi Rion,

    Ill have to look around for that, I dont think Ive gotten to that class yet. Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 1, 2013 4:00 PM