HttpContext.Current.User.Identity.Name returning null

梦想的初衷 提交于 2019-12-24 13:05:09

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/7246496/httpcontext-current-user-identity-name-returning-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!