Asp.net mvc HttpContext.Current.User.Identity.Name返回null

Asp.net mvc HttpContext.Current.User.Identity.Name返回null,asp.net-mvc,windows-authentication,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) {

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"];
}

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

希望这有帮助

编辑:-

请参考paolo在上面发布的链接。它有一个在ASP.NETMVC中使用FormsAuth的好例子。我已经有一段时间没有使用ASP.NET MVC了,但我认为您的问题是,在对用户进行身份验证后,从未填充过HttpContext.Current.User。请确保在身份验证功能中使用FormsAuthentication.SetAuthCookie和FormsAuthentication.RedirectFromLoginPage


另外,从您上面的问题来看,您可能正在使用AD作为安全存储。您可能希望研究使用Windows身份验证,因为这将自动填充HTTPContext.Current.User。

尝试查看此其他问题身份验证模式=“表单”