开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜