【问题标题】:InvalidCastException when creating a mailbox enabled user in active directory在 Active Directory 中创建启用邮箱的用户时出现 InvalidCastException
【发布时间】:2012-03-21 10:40:42
【问题描述】:

我有一个 C# (Visual Studio 2010) 应用程序来管理 Microsoft Active Directory 中的对象。如果我想创建一个启用邮箱的用户,此时我会收到一个 InvalidCastException:

IMailboxStore mailbox = (IMailboxStore)NewUser.NativeObject;

这是我的完整代码:

public void CreateUser(string Path,
                       string sAdminUserName,
                       string sAdminUserPassword,
                       string LastName,
                       string FirstName,
                       string sUserName,
                       string sUserPassword,
                       string sHomeMDB)
{
    DirectoryEntry dirEntry = new DirectoryEntry(Path, sAdminUserName, sAdminUserPassword);
    DirectoryEntry NewUser = dirEntry.Children.Add("CN=" + LastName + "." + FirstName, "user");

    NewUser.Properties["samAccountName"].Value = sUserName;

    NewUser.CommitChanges();
    NewUser.Invoke("SetPassword", new object[] { sUserPassword});
    NewUser.Properties["userAccountControl"].Value = 0x200;
    NewUser.CommitChanges();

    try
    {
        IMailboxStore mailbox = (IMailboxStore)NewUser;
        mailbox.CreateMailbox(sHomeMDB);
        NewUser.CommitChanges();
    }
    catch (InvalidCastException e)
    {
        MessageBox.Show(e.Message.ToString());
    }
}

我正在一个不是交换服务器的客户端上开发。这就是我安装 Exchange 管理工具的原因。但不幸的是,这个错误仍然发生。

谁能帮帮我?

提前致谢。

【问题讨论】:

  • 你的代码调用的是(IMailboxStore)NewUser而不是(IMailboxStore)NewUser.NativeObject
  • 感谢您的回复。我也试过你的版本,但我得到了同样的错误。
  • 您是如何获得互操作性的?您是否在运行代码的机器上安装了“Exchange 管理组件”?
  • 嗯我已经安装了Exchange System Manager. 你是这个意思吗?
  • 我不太确定。在我看来,您根本没有在您的机器中注册该 COM 接口。你是怎么得到互操作的?你是从某处下载的还是从 tlbimp 生成的?

标签: c# active-directory exchange-server


【解决方案1】:

虽然这个问题已经很老了,但我想为我不久前发现的问题发布一个解决方案。也许这可以帮助其他有同样问题的人。

在项目属性 -> 配置管理器 -> 活动解决方案平台中,我选择了 64 位平台。这就是问题发生的原因。 64 位的 IMailboxStore 接口不存在。所以我把设置改回32位,错误就消失了。

【讨论】:

    猜你喜欢
    • 2017-10-11
    • 2012-06-05
    • 2014-06-15
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多