0

我在 Windows 域上有一个 Intranet 服务器(服务器是 Windows 2003、IIS6、NTFS 权限)。它在域 Domain01 上。我有来自同一林中两个域的用户访问此 Intranet:Domain01 和 Domain02(DC 也运行 Windows 2003)。目前,用户需要通过输入以下任一方式登录:Domain01\username 或 username@Domain01

我的用户每次登录时都必须输入域,这让他们感到完全和彻底的困惑。有没有什么方法可以让他们只输入用户名和密码而不输入域来登录?比如让服务器默认尝试Domain01,如果登录失败则尝试Domain02?

注意:如果可能的话,我想通过 IIS 或服务器设置而不是通过编程来执行此操作(作为参考,我使用的是 ASP.NET 2.0)。

4

1 回答 1

2

是的。通常我所做的是使用提供的用户名作为 sAMAccountName 进行全局目录搜索。使用 PrincipalSearcher 执行此操作需要获取底层 DirectorySearcher 并将其替换为 SearchRoot。一旦找到相应的用户对象,我就会从用户对象的路径中提取域并将其用作身份验证步骤的域。您如何进行身份验证取决于您需要它执行的操作。如果您不需要模拟,您可以使用PrincipalContext.ValidateCredentials来确保用户名/密码使用PrincipalContext匹配,该 PrincipalContext 与您之前找到的用户帐户的域相匹配。如果您需要模拟,请查看此参考

// NOTE: implement IDisposable and dispose of this if not null when done.
private DirectoryEntry userSearchRoot = null;
private UserPrincipal FindUserInGlobalContext( string userName )
{
    using (PrincipalSearcher userSearcher = new PrincipalSearcher())
    {
        using (PrincipalContext context
                 = new PrincipalContext( ContextType.Domain ))
        {
            userSearcher.QueryFilter = new UserPrincipal( context );
            DirectorySearcher searcher
                 = (DirectorySearcher)userSearcher.GetUnderlyingSearcher();

            // I usually set the GC path from the existing search root
            // by doing some string manipulation based on our domain
            // Your code would be different.
            string GCPath = ...set GC path..

            // lazy loading of the search root entry.  
            if (userSearchRoot == null)
            {
                userSearchRoot = new DirectoryEntry( GCPath );
            }

            searcher.SearchRoot = userSearchRoot;
            using (PrincipalContext gcContext =
                     new PrincipalContext( ContextType.Domain,
                                           null,
                                           GCPath.Replace("GC://",""))
            {
                UserPrincipal userFilter = new UserPrincipal( gcContext );
                userFilter.SamAccountName = userName;
                userSearcher.QueryFilter = userFilter;
                return userSearcher.FindOne() as UserPrincipal;
            }
        }
    }
}
于 2009-02-03T16:51:07.310 回答