可以“Context.User”吗?为空?
在我的 MasterPage 代码隐藏中,我尝试获取经过身份验证的用户 ID(如果有):
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;
}
}
一旦出现错误“对象引用未设置到对象的实例”。我怀疑问题出在 Context.User=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 Context.User=null. Could it be the reason?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许用户在经过身份验证时被数据库删除,因此
Membership.GetUser
返回null
并且user.ProviderUserKey
抛出NullReferenceException
。Maybe the user was deleted by the db while he was authenticated, so
Membership.GetUser
returnednull
anduser.ProviderUserKey
has thrownNullReferenceException
.