Windows身份验证使用WindowsIdentity和CurrentPrincipal的ASP.NET-IIS7网页

时间:2013-12-13 20:29:17

标签: c# asp.net

使用web.config中的以下条目

<authentication mode="Windows" />
<identity impersonate="true" />

这行代码

System.Security.Principal.WindowsIdentity.GetCurrent().Name; 

返回NT AUTHORITY\\IUSR

如果没有这些条目,则返回:IIS APPPOOL\\ASP.NET V4.0

如果我使用..

System.Threading.Thread.CurrentPrincipal.Identity.Name;

返回""

还尝试使用..

进行设置
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
在应用开始时

结果仍然相同""

我在这里错过了什么吗?

我删除了impersonate = True并启用了iis中的Windows身份验证模式并重置了iis。 我正在获取IIS APPPOOL \ ASP.NET v4.0 for identity.name。

这是我的身份验证设置。 enter image description here

4 个答案:

答案 0 :(得分:0)

您是否尝试使用Windows身份验证来模拟访问该网页的用户?即,&#34; DOMAIN \ JohnDoe&#34; ?

如果是,您需要删除

<identity impersonate="true" />

该行导致Windows身份验证在IIS服务器上模拟Windows用户。 &#34; NT AUTHORITY \ IUSR&#34;是您服务器上的系统用户。

答案 1 :(得分:0)

这可能不是您要找的,但如果您只是想看到连接的域/用户名,我使用(vb):

dim s as String = Me.User.Identity.Name

C#我相信只是

string s = this.User.Identity.Name;

答案 2 :(得分:0)

将此添加到web.config解决了该问题。

<authorization> 
<deny users="?" /> 
</authorization>

答案 3 :(得分:0)

尝试以下

  • 将模拟设置为true
  • 将Windows身份验证设置为true
  • 禁用匿名

在代码中,如果要获取Domain \ User,请使用以下

    System.Security.Principal.IPrincipal User = System.Web.HttpContext.Current.User;     
    String username = User.Identity.Name.ToString()

加成: 我发现下面的代码对于跟踪身份验证属性非常有用

http://blogs.msdn.com/b/friis/archive/2013/01/08/asp-net-authentication-test-page.aspx