简体   繁体   English

ASP.Net(IIS 7.5)查询没有用户凭据的Active Directory

[英]ASP.Net (IIS 7.5) Querying Active Directory without User Credentials

I have multiple web applications that I've built for our intranet. 我有多个我为内联网构建的Web应用程序。 I wanted to allow users to not worry about logging in, so these apps pull the currently logged on user when they hit the site. 我想让用户不必担心登录,因此这些应用程序会在当前登录的用户访问网站时将其拉出。 I used this code for this to happen: 我使用此代码实现此目的:

Dim userName As String = User.Identity.Name
CurrentUser = userName.Substring(userName.IndexOf("\") + 1)

This works like a charm, no issues here. 这就像一个魅力,这里没有问题。 The next step is to query Active Directory for that logged in user to pull various information. 下一步是查询该登录用户的Active Directory以提取各种信息。 How I currently have it coded, it works like a charm on the devleopment side (typical because I'm not running IIS). 我目前如何编码,它的工作方式就像在devleopment方面的魅力(典型的是因为我没有运行IIS)。

The problem becomes when I publish it to my IIS server (Windows Server 2008 R2 running IIS 7.5), I get error messages that point to the specific line in my code that queries Active Directory. 当我将其发布到我的IIS服务器(运行IIS 7.5的Windows Server 2008 R2)时出现问题,我收到的错误消息指向我的代码中查询Active Directory的特定行。 The interesting part is these apps were working great last week. 有趣的是,这些应用程序上周运行良好。 They broke after my server admin did the latest batch of Windows Updates (please note, I am running them using .Net Framework 4.0) 在我的服务器管理员执行了最新一批Windows更新后,他们破了(请注意,我使用.Net Framework 4.0运行它们)

Before I had each app setup so that Windows Authentication was Enabled, the other Authentication types were disabled. 在我为每个应用程序设置以便启用Windows身份验证之前,其他身份验证类型已被禁用。 For providers, Negotiate is #1, NTLM is #2. 对于提供商,Negotiate是#1,NTLM是#2。 For Advanced Settings, Extended Protection = Off, and Enable Kernel-mode authentication is checked. 对于高级设置,请选中“扩展保护=关闭”和“启用内核模式身份验证”。

My web.config has the following set: 我的web.config有以下设置:

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

These were the settings I had, and everything worked like a charm. 这些是我的设置,一切都像魅力一样。 Now to get it to somewhat work I need to swap the providers around so NTLM is #1 and Negotiate is #2. 现在要让它有点工作我需要交换提供商,所以NTLM是#1,Negotiate是#2。 Because of this, the user's credentials are not properly passed in and the AD query fails. 因此,用户的凭据未正确传入,并且AD查询失败。 This is the coding I'm using for the query: 这是我用于查询的编码:

Dim adSearchRoot As New DirectoryEntry("LDAP://DC=[DOMAIN],DC=com")
Dim adSearch As New DirectorySearcher(adSearchRoot)

adSearch.Filter = "(&(ObjectClass=User)(sAMAccountName=" & CurrentUser & "))"
Dim searchResult As SearchResult = adSearch.FindOne()

Ever since the updates, when loading the site with Negotiate in front, it fails on that bottom line because I don't have a username/password set for the DirectoryEntry. 自更新以来,当前面的Negotiate加载网站时,它在底线上失败,因为我没有为DirectoryEntry设置用户名/密码。 Even when I set a username/password, it still does not 100% work like it used to. 即使我设置了用户名/密码,它仍然不像过去那样100%工作。

So my question becomes, what do I need to do so that the user accesses the site, I can know their username, and can query active directory without requiring the use of a username/password in the DirectoryEntry?? 所以我的问题变成了,我需要做什么才能让用户访问网站,我可以知道他们的用户名,并且可以查询活动目录而无需在DirectoryEntry中使用用户名/密码?

Is it a setting in IIS? 它是IIS中的设置吗? Or do I need to recode? 或者我需要重新编码? web.config setting perhaps? 也许是web.config设置? Do I need to revert server updates and figure out which one causes the break to occur? 我是否需要还原服务器更新并确定哪一个导致中断发生?

Thanks for the advice in advance. 感谢您的建议。 If you have any questions to help answer the question, let me know. 如果您有任何问题要帮助回答这个问题,请告诉我。

UPDATE UPDATE

I tried as Matt suggested by adding the following clip to my web.config file: 我尝试通过将以下剪辑添加到我的web.config文件中来建议Matt:

<security>
  <authorization>
    <add accessType="Deny" users="?" />
  </authorization>
</security>

This did not work. 这没用。 I did some reading, and then altered this section further: 我做了一些阅读,然后进一步修改了这一部分:

<location path="Default Web Site/NameOfApp">
  <system.webServer>
    <security>
      <authentication>
        <anonymousAuthentication enabled="false"/>
        <windowsAuthentication enabled="true">
          <providers>
            <add value="Negotiate" />
            <add value="NTLM" />
          </providers>
        </windowsAuthentication>
      </authentication>
      <authorization>
        <add accessType="Deny" users="?" />
      </authorization>
    </security>
  </system.webServer>
  </location>

In doing this I also removed the string that was higher up in my web.config section. 在这样做的过程中,我还删除了web.config部分中较高的字符串。 This did not work either (fyi, this was a great reference http://www.iis.net/ConfigReference/system.webServer/security/authentication ) 这也不起作用(fyi,这是一个很好的参考http://www.iis.net/ConfigReference/system.webServer/security/authentication

I then tripped across this article: http://social.technet.microsoft.com/Forums/en/winserverDS/thread/7deba16b-295a-4887-98f9-9f291ed49871 which seemed to be a similar situation. 然后我跳过这篇文章: http//social.technet.microsoft.com/Forums/en/winserverDS/thread/7deba16b-295a-4887-98f9-9f291ed49871 ,这似乎是一个类似的情况。 This article eventually referenced "Double Hops", after looking into this and trying a few things, this didn't solve my issue either. 本文最后引用了“Double Hops”,在研究了这个并尝试了一些事情之后,这也没有解决我的问题。

Next Step 下一步

I am going to try a new IIS 7.5 implementation on a different Server 2008 R2 system and essentially start from scratch, to see if the problem recreates or not. 我将在不同的Server 2008 R2系统上尝试新的IIS 7.5实现,并且基本上从头开始,以查看问题是否重新创建。

ANY new suggestions would be of great help. 任何新的建议都会有很大的帮助。

I was able to make the code execute without any problems on a Windows 2008 Server. 我能够在Windows 2008 Server上执行代码而不会出现任何问题。 I created a new .NET 4.0 application pool and assigned it to the web application. 我创建了一个新的.NET 4.0应用程序池并将其分配给Web应用程序。 I changed the web.config to deny anonymous access and use Windows authentication. 我更改了web.config以拒绝匿名访问并使用Windows身份验证。 The code executed without exception. 代码执行无例外。

Looking at your web.config clip, I wonder if this might be what you're missing: 看看你的web.config剪辑,我想知道这可能是你所缺少的:

  <system.webServer>
    <security>
      <authorization>
        <add accessType="Deny" users="?" />
      </authorization>
    </security>
  </system.webServer>

It's important that you have this authorization section within the system.webServer section. system.webServer部分中拥有此授权部分非常重要。 IIS 7 uses the system.webServer section to store some settings that were part of the metabase in IIS 6. IIS 7使用system.webServer部分存储一些属于IIS 6中元数据库的设置。

I had this same problem. 我有同样的问题。 Here's how I solved it: 这是我解决它的方式:

Use overload 4 of 5 for the DirectoryEntry constructor which allows for not only a path, but a username, and a password as well. 对DirectoryEntry构造函数使用重载4 of 5,它不仅允许路径,还允许用户名和密码。 So, your AD connection should look like this: 因此,您的AD连接​​应如下所示:

DirectoryEntry adSearchRoot = new DirectoryEntry("LDAP://DC=[DOMAIN],DC=com", "DOMAIN\Username", "Password");
DirectorySearcher adSearch = new DirectorySearcher(entry);

Now, instead of "DOMAIN\\Username", use a service account and then for the password, obviously, use the password for the service account. 现在,使用服务帐户代替“DOMAIN \\ Username”,然后使用密码,显然,使用服务帐户的密码。

I think as long as the service account is a member of the "Domain Users" group, then you should be able to query AD no problem. 我认为只要服务帐户是“域用户”组的成员,那么您应该能够查询AD没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM