【问题标题】:Setting Office 365 out of office reply for another user with C#使用 C# 为另一个用户设置 Office 365 外出回复
【发布时间】:2018-11-28 17:44:33
【问题描述】:

我正在尝试编写一个 C# 控制台应用程序,它将为 Office 365 上的用户设置外出回复。假设我有一个帐户 alice@domain.com 在 O365 上具有全局管理员权限,试图将 OOF 回复设置为非管理员bob@domain.com.

可以使用 Powershell 成功获取和设置 Bob 的 OOF,使用以下命令:

$UserCredential = Get-Credential #Sign in as Alice
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session

Get-MailboxAutoReplyConfiguration -Identity bob@domain.com
Set-MailboxAutoReplyConfiguration -Identity bob@domain.com -AutoReplyState Enabled -InternalMessage "I'm out of office"

所以我尝试使用以下代码在我的 C# 应用程序中做类似的事情:

SecureString password = ...

var service = new ExchangeService
{
    Credentials = new NetworkCredential("alice@domain.com", password),
    Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx")
};

var settings = service.GetUserOofSettings("bob@domain.com");
...
service.SetUserOofSettings("bob@domain.com", newSettings)

但是,对于 get 和 set,我最终都得到了 Microsoft.Exchange.WebServices.Data.ServiceResponseException 和消息 'Access is denied. Check credentials and try again., The process failed to get the correct properties.'。另请注意,我可以使用此方法成功获取和设置 Alice 的 OOF,而不是 Bob 的。有谁知道为什么我有权使用 Powershell 而不是 C# 来执行此操作,以及我该如何解决这个问题?

我也尝试设置服务的ImpersonatedUserId 字段,但随后我收到错误消息'The account does not have permission to impersonate the requested user.',我真的不想让 Alice 模拟我需要设置其 OOF 的每个用户.

它需要在 C# 中工作,因为我在我的应用程序中做了很多其他的事情,这些事情在 Powershell 脚本中编写起来并不容易。我能想到的唯一另一件事就是使用我的 C# 代码直接发送 Powershell 命令,详见this post,但这似乎有点冗长且容易出错。

【问题讨论】:

    标签: c# powershell office365 exchangewebservices


    【解决方案1】:

    当您使用 Exchange 命令行管理程序 cmdlet Set-MailboxAutoReplyConfiguration 时,您可以利用通过 RBAC 提供的委派。然而,EWS 是一个用户邮箱 API,RBAC 权限没有任何意义,因此作为全局管理员(或任何类型的管理员)对这个 API 没有任何意义。这与 MAPI、ActiveSync 相同。

    为了能够通过 EWS 设置 OOF 设置,您需要通过 Add-MailboxPermission 授予对目标邮箱的完全邮箱访问权限。然而,更好的选择是使用 C# 中的 EMS cmdlet,例如

                String UserName = "username@om";
            String Password = "dd@#";
            System.Security.SecureString secureString = new System.Security.SecureString();
            foreach (char c in Password)
                secureString.AppendChar(c);
            PSCredential credential = new PSCredential(UserName, secureString);
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://outlook.office365.com/powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
            connectionInfo.SkipCACheck = true;
            connectionInfo.SkipCNCheck = true;
    
            connectionInfo.MaximumConnectionRedirectionCount = 4;
            Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
            runspace.Open();
            // Make a Get-Mailbox requst using the Server Argument
            Command gmGetMailbox = new Command("Get-MailboxAutoReplyConfiguration");
            gmGetMailbox.Parameters.Add("Identity", "jcool@datarumble.com");
            Pipeline plPileLine = runspace.CreatePipeline();
            plPileLine.Commands.Add(gmGetMailbox);
            Collection<PSObject> RsResultsresults = plPileLine.Invoke();
            foreach (PSObject obj in RsResultsresults)
            {
                Console.WriteLine(obj.Members["AutoReplyState"].Value.ToString());
            }
            plPileLine.Stop();
            plPileLine.Dispose();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多