Click to See Complete Forum and Search --> : Determine if user logged on?


kobus
June 6th, 2006, 01:58 AM
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

Rohit Kukreti
June 7th, 2006, 03:33 AM
To use "User" property you need to import System.Security namespace
Also, you can access the session using Session["loginStatus"].