首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UserPrincipal.GetGroups()方法在更改DomainController后抛出异常

UserPrincipal.GetGroups()方法在更改DomainController后抛出异常
EN

Stack Overflow用户
提问于 2019-05-02 06:51:43
回答 1查看 604关注 0票数 2

我们有一个winforms应用程序,它从活动目录中获取组。现在一切都很顺利,直到我们把旧的领域控制器换成了一个新的。

从那时起,我们的应用程序在调用UserPrincipal.GetGroups()方法时抛出一个异常。引发异常是因为它试图连接到旧的DC。

异常消息被翻译成:

服务器不运行surottdc04.TOSOT.CH

有没有人知道,如果旧的dc-信息被缓存在某个地方,或者应用程序从哪里获得旧信息?

在下面的屏幕快照中,您可以看到抛出异常的代码部分:

如您所见,在“监视”窗口中,有正确的新DC surottdc06,这很可能是从当前日志记录用户的上下文中获取的。但在例外情况下,仍然存在旧的DC surottdc04,为什么?

更新

到目前为止,我们已经发现,当我们将上下文作为参数传递给该方法时,它可以工作,但是没有上下文,该方法试图连接到旧的DC。

这是一个可能的解决方案,但问题仍然是,当方法被称为无参数时,该方法从哪里获得旧的DC信息并试图连接到那里?

代码语言:javascript
复制
public void GetGroups()
{

    var sid = WindowsIdentity.GetCurrent().User.Value;

    using (var context = new PrincipalContext(ContextType.Domain, "tosot.ch"))
    {
        using (var userPrinciple = UserPrincipal.FindByIdentity(context, sid))
        {
            /*
                * this works, we just pass the context which we've used to 
                * create the UserPrincipal instance again to fetch the groups
                */
            var ret = userPrinciple.GetGroups(context);


            /*
                * this works NOT: when calling without context argument,
                * it seems, the context used is not the same 
                * as the userPrinciple instance is linked to.
                * Instead it uses a selfmade context with an yet exsting,
                * but currently not online domain controller - why that??
                * (this 'old' domain controller is currently not running, 
                *  but it's yet not removed from the domain ...)
                */
            ret = userPrinciple.GetGroups();
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-05-08 14:00:17

我猜DNS还在返回旧DC的IP。

在命令行中,运行:

代码语言:javascript
复制
nslookup tosot.ch

你看到surottdc04的IP了吗?如果是的话,那就是你的问题。

我经历过这个问题,虽然我从来没有能力去解决它。这些说明可能会有所帮助,但它看起来像一篇旧文章,因此可能不再采用相同的方式了:https://support.microsoft.com/en-us/help/555846

更新:或者您可以使用C#查看它为当前域看到的DC。看看旧的是否还会出现:

代码语言:javascript
复制
foreach (DomainController dc in Domain.GetCurrentDomain().DomainControllers) {
    Console.WriteLine(dc.Name);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55947060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档