1

The System.Web.HttpContext.Current.User.Identity.Name is returning "null."

I am definitely logged in.

How can this be solved?

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"];
}
p.campbell
  • 98,673
  • 67
  • 256
  • 322
Susan
  • 1,822
  • 8
  • 47
  • 69
  • 1
    try taking a look at this other question [HttpContext.Current.User.Identity.Name is always string.Empty](http://stackoverflow.com/questions/1056487/httpcontext-current-user-identity-name-is-always-string-empty) – Paolo Falabella Aug 30 '11 at 16:30

1 Answers1

3

What kind of authentication do you have configured in your web.config? Also, are you using IIS or the dev web server? You may need to enable forms auth or windows auth in IIS or your web.config.

Hope this helps.

Edit:-

Please refer to the link that paolo posted above. It has a decent example of using forms auth in ASP.NET MVC. I haven't played with ASP.NET MVC in a while but I think your problem is that HttpContext.Current.User was never populated after your user was authenticated. Please ensure that you are using FormsAuthentication.SetAuthCookie and FormsAuthentication.RedirectFromLoginPage in your authentication function.

Also, from your question above, it seems like you might be using AD as your security store. You may want to look into using Windows Authentication as that should automatically populate HTTPContext.Current.User.

Nabheet
  • 1,285
  • 1
  • 12
  • 22