开发者

Could "Context.User" be a null?

开发者 https://www.devze.com 2023-01-09 23:48 出处:网络
In my MasterPage code-behind I try to get UserID of the authenticated (if it has) one: public Guid CurrentUserID

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

精彩评论

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