首页 > 解决方案 > 如何使用 c# 在 Exchange 服务器中获取用户对日历的权限

问题描述

我在我的 Web 应用程序中使用交换日历。在将事件从我的 Web 应用程序插入到日历以交换服务器之前,我想在我的 c# 端获取该特定日历的用户权限。我通过执行以下 powershell 获得了权限命令。但坚持使用 c# 得到相同的结果。

Get-MailboxFolderPermission -Identity john@contoso.com:\Calendar -User "test@test.com"

标签: c#powershelloffice365exchange-server

解决方案


如果不调用 powershell,我找不到任何方法,但可以这样做:

var runspace = RunspaceFactory.CreateRunspace();
runspace.Open();

object psSessionConnection;

// Create a powershell session for remote exchange server
using (var powershell = PowerShell.Create())
{
    var command = new PSCommand();
    command.AddCommand("Get-MailboxFolderPermission");
    command.AddParameter("Identity", "john@contoso.com:\Calendar");
    command.AddParameter("User", "test@test.com"));
    powershell.Commands = command;
    powershell.Runspace = runspace;

    var result = powershell.Invoke();
    psSessionConnection = result[0];
}

您正在做的是从代码本身实际运行 PS 命令。

我还找到了EWS api,它可能包含您要查找的内容。

使用 Bind 方法获取文件夹的当前权限。

    // Create a property set to use for folder binding.
    PropertySet propSet = new PropertySet(BasePropertySet.FirstClassProperties, FolderSchema.Permissions);
    // Bind to the folder and get the current permissions. 
    // This call results in a GetFolder call to EWS.
    Folder sentItemsFolder = Folder.Bind(service, new FolderId(WellKnownFolderName.Calendar, "test@test.com"), propSet);

推荐阅读