Could "Context.User" be a null?
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 Cont开发者_如何学Cext.User=null. Could it be the reason?
Maybe the user was deleted by the db while he was authenticated, so Membership.GetUser
returned null
and user.ProviderUserKey
has thrown NullReferenceException
.
精彩评论