【发布时间】: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