HttpContext.Current.User.Identity.Name返回null

时间:2011-08-30 16:25:24

标签: asp.net-mvc windows-authentication

System.Web.HttpContext.Current.User.Identity.Name返回“null”。

我绝对是登录的。

如何解决这个问题?

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
     base.OnActionExecuting(filterContext);

     if (Session["FirstName"] == null)
     {
         string loginName = System.Web.HttpContext.Current.User.Identity.Name;
         string networkId = loginName.Split('\\')[1];
         Session["FirstName"] = new AD_Utility().FirstName(networkId);
     }
     ViewBag.FirstName = Session["FirstName"];
}

1 个答案:

答案 0 :(得分:3)

您在web.config中配置了哪种身份验证?另外,您使用的是IIS还是开发Web服务器?您可能需要在IIS或web.config中启用表单auth或windows auth。

希望这会有所帮助。

编辑: -

请参阅上面发布的paolo链接。它有一个很好的例子,在ASP.NET MVC中使用表单auth。我有一段时间没有使用过ASP.NET MVC,但我认为你的问题是在用户通过身份验证后从未填充过HttpContext.Current.User。请确保您在身份验证功能中使用FormsAuthentication.SetAuthCookie和FormsAuthentication.RedirectFromLoginPage。

此外,根据您的上述问题,您似乎可能正在使用AD作为安全存储。您可能希望使用Windows身份验证,因为它应该自动填充HTTPContext.Current.User。

相关问题