HttpContext.Current.User.Identity.Name返回null [英] HttpContext.Current.User.Identity.Name returning null

查看:1373
本文介绍了HttpContext.Current.User.Identity.Name返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Web.HttpContext.Current.User.Identity.Name 将返回空。

我绝对登录。

这又如何解决呢?

 保护覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
{
     base.OnActionExecuting(filterContext);     如果(会话[名字] == NULL)
     {
         字符串LOGINNAME = System.Web.HttpContext.Current.User.Identity.Name;
         字符串NETWORKID = loginName.Split('\\\\')[1];
         会话[名字] =新AD_Utility()名字(NETWORKID)。
     }
     ViewBag.FirstName =会话[名字];
}


解决方案

什么样的​​身份验证你已经在你的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。

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

解决方案

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.

这篇关于HttpContext.Current.User.Identity.Name返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆