我刚刚改用 msbuild 来预编译我的网站,现在我收到了这个奇怪的错误:
我调用了 Membership.GetUser() ,它抛出:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.Security.Membership.GetCurrentUserName() +36
System.Web.Security.Membership.GetUser() +7
...
最佳答案
Reflector 显示 Membership.GetCurrentUserName 的实现是:
private static string GetCurrentUserName()
{
if (HostingEnvironment.IsHosted)
{
HttpContext current = HttpContext.Current;
if (current != null)
{
return current.User.Identity.Name;
}
}
IPrincipal currentPrincipal = Thread.CurrentPrincipal;
if ((currentPrincipal != null) && (currentPrincipal.Identity != null))
{
return currentPrincipal.Identity.Name;
}
return string.Empty;
}
乍一看,最可能的解释是:
HttpContext.Current 不为 null,并且
HttpContext.Current.User 为 null 或具有 null Identity 属性。
所有其他路径似乎都有 null 测试。
所以我建议您跟踪 HttpContext.User 的类型和内容。
HttpContext.Current.User 是一个 IPrincipal,据我所知,IPrincipal 的大多数具体实现都不允许 null 标识,因此我打赌 HttpContext.User 为 null。
关于msbuild - 为什么我从 Membership.GetCurrentUserName 收到 NullReferenceException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/663018/