views:

50

answers:

1

In my MasterPage code-behind I try to get UserID of the authenticated (if it has) one:

public Guid CurrentUserID
    {
        get
        {
            Guid userID = new Guid();
            if (Context.User.Identity.IsAuthenticated)
            {
                MembershipUser user = Membership.GetUser(Context.User.Identity.Name);
                userID = (Guid)user.ProviderUserKey;
            }
            return userID;
        }
    }

Once the error "Object reference not set to an instance of an object" appeared. I suspect the problem is in the case Context.User=null. Could it be the reason?

A: 

Maybe the user was deleted by the db while he was authenticated, so Membership.GetUser returned null and user.ProviderUserKey has thrown NullReferenceException.

onof
Yep, it seems to be logic but it's not the case. Probably cookie file was deleted. Could it be the reason?
landless
if cookie is deleted the user is no more authenticated.
onof