2014-03-04 26 views
1

我环顾四周,仍然没有得到答案。ASP.Net在全局文件里面得到userid

我有一个ASP.NET应用程序,它使用Windows身份验证。 web.config中的配置是否正确:

<authentication mode="Windows"/> 
    <authorization> 
     <deny users="?"/> 
    </authorization> 

我得到在方法的用户ID: WindowsAuthentication_Authenticate(对象发件人,WindowsAuthenticationEventArgs E)

被获取的用户ID的代码:

var userId = String.Empty; 
     //Dispalys the UserName from AD. 
     System.Security.Principal.IPrincipal User; 
     User = System.Web.HttpContext.Current.User; 
     if (User != null) 
     { 
      string username = User.Identity.Name; 
      username = username.Substring(username.IndexOf("\\") + 1); 

      CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture; 
      TextInfo textInfo = cultureInfo.TextInfo; 
      userId = (textInfo.ToTitleCase(username)); 
     } 

在这种情况下,System.Web.HttpContext.Current.User始终为空。

我在这里做错了什么?

回答

2

WindowsAuthentication_Authenticate方法的参数之一是WindowsAuthenticationEventArgs类型。其中一个属性是Identity,因此您应该可以通过编写e.Identity.Name来访问用户名。

参考链接: