2011-08-30 48 views
1

System.Web.HttpContext.Current.User.Identity.Name返回“null”。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

试着看看这个其他问题[HttpContext.Current.User.Identity.Name总是string.Empty](http://stackoverflow.com/questions/1056487/httpcontext-current-user-identity-name- is-always-string-empty) –

回答

3

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

希望这会有所帮助。

编辑: -

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

另外,从您上面的问题来看,您似乎可能会将AD用作您的安全商店。您可能想要考虑使用Windows身份验证,因为它应该自动填充HTTPContext.Current.User。

+1

身份验证模式=“形式” – Susan