使用c#

时间:2018-06-04 15:19:19

标签: c# exchangewebservices service-accounts microsoft-exchange

我必须与Microsoft Exchange Webservice建立连接,并且我已经获得了以下详细信息 -
共享邮箱地址是 -

  

“students@student.edu”

服务帐户是 -

  

“学生SA”

服务帐户的密码是 -

  

“Pass1234”

我按照网站https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/get-started-with-ews-managed-api-client-applications

中提供的代码示例进行操作

以下是使用上述详细信息的代码示例 -

static void Main(string[] args)
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);

            service.Credentials = new WebCredentials("Student SA", "Pass1234");

            service.TraceEnabled = true;
            service.TraceFlags = TraceFlags.All;

            service.AutodiscoverUrl("students@student.edu", RedirectionUrlValidationCallback);

            // service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.PrincipalName, "myADAccount");    

        }

        private static bool RedirectionUrlValidationCallback(string redirectionUrl)
        {
            // The default for the validation callback is to reject the URL.
            bool result = false;

            Uri redirectionUri = new Uri(redirectionUrl);

            // Validate the contents of the redirection URL. In this simple validation
            // callback, the redirection URL is considered valid if it is using HTTPS
            // to encrypt the authentication credentials. 
            if (redirectionUri.Scheme == "https")
            {
                result = true;
            }
            return result;
        }

当我在本地运行时,我收到以下错误消息

    <Trace Tag="AutodiscoverConfiguration" Tid="9" Time="2018-06-04 15:10:07Z">  
 Request error: The remote server returned an error: (401) Unauthorized.  
 </Trace> 

我在其他帖子How to connect to Exchange?

中寻找相同的内容

以及Code项目,但它们都以相同的方式告诉如何连接到交换webservice。

我不确定为什么我在AutodiscoverConfiguration中获取未经授权的访问权限,以及我是否使用正确的代码使用已提供的服务帐户信息连接到Exchange服务器。

帮助将不胜感激!
TIA

1 个答案:

答案 0 :(得分:0)

您的凭据格式看起来不正确您应该使用下层格式,即domain \ username或UPN请参阅https://msdn.microsoft.com/en-us/library/windows/desktop/aa380525(v=vs.85).aspx。我建议你使用UPN,因为它应该始终有效。

相关问题