Determine if user logged on? [modified]
-
Hi I am developing an e-Commerce website using ASP.NET, Visual Studio 2003 and C#. I am allowing the user to add products to a shopping cart even if not logged in (using GUID as primary key in that case). When the user then logs on the anonymous shopping cart items are moved over to his personal shopping cart (using his customerIndex as part of primary key). I am using Forms authentication. When the logon is successful I also add info to the session content
Session.Contents["custIndex"] = custIndex;
Session.Contents["loginStatus"] = Enum_LoginStatus.LoggedIn;I want to display the anonymous shopping cart when not logged on and the personal shopping cart when logged on. I am using a helper class ShopcartAccess in the same namespace as my Web App (the ShopcartAccess class is just a normal C# class and not a webform) which has the following method:
public static DataTable GetShopCartItems_All()
{
DataTable table = null;//IN HERE ADD CODE TO DETERMINE IF USER LOGGED IN OR ANON //######## bool loggedIn = false; //temp for testing if (loggedIn == true) { table = GetShopCartItems\_All(); } else //use method for anonomous database access { table = GetShopCartItems\_All\_AnonUser(); } return table;
}
At the //####### in the code above I have tried using the following to determine if user logged in, but it says the type or namespace name ‘User’ can not be found.
if (User.Identity.IsAuthenticated == true)
{
}I have also tried to access the session data, but it says the type or namespace name ‘Session’ can not be found.
bool loginStatus = (Enum_LoginStatus) Session.Contents["loginStatus"];
What namespaces should be added, because I have added everything that I thought could help:
using System;
using System.Net;
using System.Data;
using System.Data.Common;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;Is there a better way of determining whether the user is logged on? Thanks. Kobus -- modified at 2:02 Tuesday 6th June, 2006
-
Hi I am developing an e-Commerce website using ASP.NET, Visual Studio 2003 and C#. I am allowing the user to add products to a shopping cart even if not logged in (using GUID as primary key in that case). When the user then logs on the anonymous shopping cart items are moved over to his personal shopping cart (using his customerIndex as part of primary key). I am using Forms authentication. When the logon is successful I also add info to the session content
Session.Contents["custIndex"] = custIndex;
Session.Contents["loginStatus"] = Enum_LoginStatus.LoggedIn;I want to display the anonymous shopping cart when not logged on and the personal shopping cart when logged on. I am using a helper class ShopcartAccess in the same namespace as my Web App (the ShopcartAccess class is just a normal C# class and not a webform) which has the following method:
public static DataTable GetShopCartItems_All()
{
DataTable table = null;//IN HERE ADD CODE TO DETERMINE IF USER LOGGED IN OR ANON //######## bool loggedIn = false; //temp for testing if (loggedIn == true) { table = GetShopCartItems\_All(); } else //use method for anonomous database access { table = GetShopCartItems\_All\_AnonUser(); } return table;
}
At the //####### in the code above I have tried using the following to determine if user logged in, but it says the type or namespace name ‘User’ can not be found.
if (User.Identity.IsAuthenticated == true)
{
}I have also tried to access the session data, but it says the type or namespace name ‘Session’ can not be found.
bool loginStatus = (Enum_LoginStatus) Session.Contents["loginStatus"];
What namespaces should be added, because I have added everything that I thought could help:
using System;
using System.Net;
using System.Data;
using System.Data.Common;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;Is there a better way of determining whether the user is logged on? Thanks. Kobus -- modified at 2:02 Tuesday 6th June, 2006
Hi, You should only add reference of
System.Web
and then get current request context usingHttpContext.Current
and then can check either session or user properties like thisHttpContext.Current.User.Identity.IsAuthenticated
. Hope this will solve your issue. Ghuna Singh -
Hi, You should only add reference of
System.Web
and then get current request context usingHttpContext.Current
and then can check either session or user properties like thisHttpContext.Current.User.Identity.IsAuthenticated
. Hope this will solve your issue. Ghuna Singh