繁体   English   中英

IIS在多域林中使用ASP.NET应用的Windows身份验证问题

[英]Windows Authentication issue in IIS using ASP.NET app in a multi-domain forest

我正在尝试实现一个使用Windows身份验证的基于Web的Intranet项目。 Web服务器(Windows 2012 Server)位于域A中,但我需要能够从林中任何域中的计算机访问该站点。 我只是使用域A和B中的计算机对此进行测试。

在IIS 7.0中,我启用了Windows身份验证,并禁用了所有其他身份验证,包括匿名身份验证。 在web.config中,我有:

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

经过身份验证的用户必须位于AD组“ TestGroup”中; 我出于测试目的在web.config中删除了<allow groups =“ TestGroup” />; 我还在主页上添加了一些标签来显示我的用户ID,我所属的组,“ TestGroup”的所有成员以及是否是“ TestGroup”的成员(出于调试目的)。

我相信到目前为止我所做的一切都正确。 使用web.config如下:

  • 当我从域A中的PC访问时,系统不会提示我登录(由于我已经登录域A,这是正确的),并且所有提示都显示正确的数据。
  • 当我从域B中的PC访问时,系统要求我(正确)登录,用户ID标签正确显示了我的ID,但没有显示用户ID的组,也没有显示“ TestGroup”中的组成员。

如果我在web.config中删除了身份部分:

  • 当从域A或域B中的PC访问时,用户ID标签显示“ NT AUTHORITY / NETWORK SERVICE”,没有列出任何属于我的组(因为我现在显然是“ NT Authority”),但是属于“ TestGroup”的组成员”正确列出。 从域B中的PC访问会正确弹出登录对话框。

如果删除授权部分并将身份部分保留在web.config中:

  • 不要求我从任一域中的PC登录;
  • 从域A中的PC访问可以正确显示所有内容
  • 从域B中的PC访问时,正确显示用户ID,但没有组成员身份,也没有列出“ TestGroup”组的用户。

似乎为了能够显示正确的用户ID,我需要将模拟设置为true。 为了要求用户从域AI之外的PC登录,AI需要具有授权部分,但两者似乎都无法在域A之外的PC上正常工作。

这是我用来获取用户ID,用户组成员身份和组成员的内容:

WindowsIdentity wiUser = WindowsIdentity.GetCurrent();
string sID = wiUser.Name.ToUpper().Repl("DomainA\\", string.Empty);
string sGroupName = @"TestGroup";
List<string> lsGroups = Utils.GetUserADGroups(sID);
bool bTC = lsGroups.Contains(sGroupName);
StringCollection scGroupMembers = Utils.GetGroupMembers(Utils.DomainType., sGroupName);

static string adDomain = "USA.ABC.DEF.COM";
static string adContainer = "DC=USA,DC=abc,DC=def,DC=com";
static string adADPath = "LDAP://USA.abc.def.com";

public static List<string> GetUserADGroups(string UserName)
{
    List<string> lsGroups = new List<string>();

    try
    {
        PrincipalContext pc = new PrincipalContext(ContextType.Domain, adDomain, adContainer);
        UserPrincipal up = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, UserName);
        PrincipalSearchResult<Principal> psr = up.GetGroups(pc);

        foreach (Principal p in psr)
        {
            lsGroups.Add(p.Name);
        }
    }
    catch (Exception)
    {
    }
    return lsGroups;
}

public static StringCollection GetGroupMembers(DomainType eDomainType, string strGroup)
{
    DirectoryEntry de = new DirectoryEntry(adDSADPath);
    System.Collections.Specialized.StringCollection GroupMembers = new System.Collections.Specialized.StringCollection();

    try
    {
        //DirectoryEntry DirectoryRoot = new DirectoryEntry(sADPath);
        DirectorySearcher DirectorySearch = new DirectorySearcher(de, ("(CN=" + (strGroup + ")")));
        SearchResultCollection DirectorySearchCollection = DirectorySearch.FindAll();

        foreach (SearchResult DirectorySearchResult in DirectorySearchCollection)
        {
            ResultPropertyCollection ResultPropertyCollection = DirectorySearchResult.Properties;

            foreach (string GroupMemberDN in ResultPropertyCollection["member"])
            {
                DirectoryEntry DirectoryMember = new DirectoryEntry(("LDAP://" + GroupMemberDN));
                System.DirectoryServices.PropertyCollection DirectoryMemberProperties = DirectoryMember.Properties;
                object DirectoryItem = DirectoryMemberProperties["sAMAccountName"].Value;

                if (null != DirectoryItem)
                {
                    GroupMembers.Add(DirectoryItem.ToString());
                }
            }
        }
    }
    catch (Exception ex)
    {
    }
    return GroupMembers;
}

我还尝试使用它来查看用户是否是该组的成员,但是如果我从域B中的PC访问该站点,则会引发错误:

public static bool IsMember(string UserName, string GroupName)
{
    try
    {
        PrincipalContext pc = new PrincipalContext(ContextType.Domain, adDomain, adContainer);
        UserPrincipal up = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, UserName);
        PrincipalSearchResult<Principal> psr = up.GetGroups(pc);

        foreach (Principal result in psr)
        {
            if (string.Compare(result.Name, GroupName, true) == 0)
                return true;
        }
        return false;
    }
    catch (Exception e)
    { 
        throw e; 
    }
}

解决我问题的最终方法是将功能包装在以下方法中:

using (System.Web.Hosting.HostingEnvironment.Impersonate())
{
}

例如,我将原始帖子中的方法从:

public static StringCollection GetGroupMembers(DomainType eDomainType, string strGroup)
{
    DirectoryEntry de = new DirectoryEntry(adDSADPath);
    System.Collections.Specialized.StringCollection GroupMembers = new System.Collections.Specialized.StringCollection();
...
}

至:

public static StringCollection GetGroupMembers(DomainType eDomainType, string strGroup)
{
    using (System.Web.Hosting.HostingEnvironment.Impersonate())
    {
        DirectoryEntry de = new DirectoryEntry(adDSADPath);
        System.Collections.Specialized.StringCollection GroupMembers = new System.Collections.Specialized.StringCollection();
        ...
    }
}

希望这可以使别人免于悲痛,因为这引起了我很多!

暂无
暂无

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

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