【发布时间】:2012-02-08 21:30:52
【问题描述】:
嗯,这是一个有趣的问题。我有一个在 Visual Studio 开发服务器上运行的 ASP.NET MVC3 Intranet 应用程序,没有 NTLM。我正在测试的机器上没有 AD 域。
当我尝试使用 User.Identity.Name 时,它会引发空引用异常,但是当我使用 Environment.UserDomain 和 Environment.UserName 时,它们会填充正确的值。
我对此感到非常困惑。我应该避免使用User.Identity.Name,还是有什么理由让它为空?
更新
我刚刚注意到,当我使用
System.Web.HttpContext.Current.User.Identity.Name 有效,但仅使用 User.Identity.Name 无效。我是否缺少 using 语句?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
【问题讨论】:
-
你运行它的代码是什么?整个上下文是什么? (方法,线程...)
-
我在 Controller 类的构造函数中直接运行这段代码
-
感谢您的更新。我遇到了同样的问题,System.Web.HttpContext.Current.User.Identity.Name 解决了它。谢谢!
标签: asp.net-mvc-3 nullreferenceexception httpcontext