Recommended way to check if user is logged in?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
14 лет назад
I'm using the following to check if  a user is logged in:


if (NopContext.Current.User == null)
{
     //do stuf
}


Is this a recommended way to check if a user is authenticated?

I WAS using:

if (HttpContext.Current.User.IsAuthenticated)
{
     //do stuf
}



I know they both work, but are the any advantages so using NopContext? Or is first chunk of code essentially a wrapper for the second?

Thanks. I LOVE nopCommerce, BTW.
14 лет назад
I would use the second.  A user could be anonyoumous (you don't have to create an account to add items to your cart) and thefore potentially the first one could pass the check but still not be logged in.  I haven't digged into the code so this is only a guess.
14 лет назад
Use the following code:

if (NopContext.Current.User == null || !NopContext.Current.User.IsGuest)
{    
     //do stuf
}
14 лет назад
Thanks guys. That helped. I was actually doing something more like this:


if (NopContext.Current.User == null || NopContext.Current.User.IsGuest)
            {
                // redirect to unauthorized page
            }
            else
            {
                try
                {

                    if (!NopContext.Current.User.IsAdmin)
                    {
                        try
                        {
                            if ( /* user does not have at least one order with a  product variant SKU matching the pattern */ )
                            {
                                // redirect to unauthorized page
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new HttpException(500, "There was a problem while determining subscription status.", ex);
                        }
                    }
                    
                }
                catch (Exception ex)
                {
                    throw new HttpException(500, "There was a problem while determining administrator status.", ex);
                }
            }


HttpContext was working fine, but I'm just trying to keep as much my code nop-centric as possible. Already getting away from it a bit with my custom LINQ data providers, but those are working well.
13 лет назад
Can anyone please tell me how NopContext.Current.User works? I am trying to customize the codes of nopCommerce and make my own application. but I failed to dig how NopContext.Current.User works?? I get the value is always null ....
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.