2017-02-27 6 views
0

私はthis resourceに従って、晴れたアクティブディレクトリで認証するアプリケーションを作成しました。この認証相互作用のトークンをExchangeとEWS管理APIを使用して、組織内のすべてのユーザーを偽装し、ログインする必要はありません。交換偽装のためのAzure ADアプリケーション専用アクセストークン

私はazureに私たちの組織にアプリを登録し、交換を許可しました。これはEWSが管理と対話するためのコードをトークンで証明書を作成し、それを私たちの紺碧のアプリケーションを設定した後、私は次のコードを経由してADALを使用してアプリ専用のアクセストークン...

string authority = "https://login.windows.net/{tenant}/oauth2/authorize"; 
AuthenticationContext authenticationContext = new AuthenticationContext(authority, false); 
var certPath = @"C:\path\to\cert\Cert.pfx"; 
var certfile = System.IO.File.OpenRead(certPath); 
var certificateBytes = new byte[certfile.Length]; 
certfile.Read(certificateBytes, 0, (int)certfile.Length); 
var cert = new X509Certificate2(
    certificateBytes, 
    PRIVATE_KEY_PASSWORD, 
    X509KeyStorageFlags.Exportable | 
    X509KeyStorageFlags.MachineKeySet | 
    X509KeyStorageFlags.PersistKeySet); 

ClientAssertionCertificate cac = new ClientAssertionCertificate(CLIENT_ID, cert); 

var token = await authenticationContext.AcquireTokenAsync("https://outlook.office365.com/", cac); 

を得ることができますAPIは、次のようになります。..

ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack; 
_ExchangeService = new ExchangeService(ExchangeVersion.Exchange2013_SP1) { 
    Credentials = new OAuthCredentials(token), 
    Url = new Uri("https://outlook.office365.com/ews/exchange.asmx"), 
    ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.PrincipalName, "[email protected]"), 
}; 

_ExchangeService.HttpHeaders.Add("X-AnchorMailbox", "[email protected]"); 

すべての要求が401不正エラーを返しますが、これは、管理APIを介して偽装を設定するための正しい方法であるように思われます。

私の質問はここまでに間違っていますか?あるいは、私のアプリケーションにExchangeサーバへのアクセスを与えるために何か必要なことがありますか?

article私が従っていたのはクライアント同意フローでしたが、そのセクションの詳細は明確ではありません。最初に同意を促すことなく、誰にでもアプリを許可することはできません。

答えて

1

、まずあなたがAzureのADにより保護されてアプリにOfficeのすべてのメールボックスアプリケーションの許可365 Exchange Onlineのへのフルアクセスを使用Exchange Webサービスを設定したことを確認してくださいテストするには、次のコードを試してみてください。

 ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2013); 
     exchangeService.Url = new Uri("https://outlook.office365.com/ews/exchange.asmx"); 
     exchangeService.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "[email protected]"); 
     exchangeService.HttpHeaders.Add("X-AnchorMailbox", "[email protected]"); 
     exchangeService.TraceEnabled = true; 
     exchangeService.TraceFlags = TraceFlags.All; 
     exchangeService.Credentials = new OAuthCredentials(token.AccessToken); 
     Folder newFolder = new Folder(exchangeService); 
     newFolder.DisplayName = "TestFolder"; 

     newFolder.Save(WellKnownFolderName.Inbox); 

ターゲットアカウントの受信トレイに新しいフォルダが作成されます。

+0

これは私が必要としていたものです。私はアプリケーションの許可を適用することを忘れてしまった。ご協力ありがとうございました。 – dannylindquist

+0

こんにちは私は同じ401のエラーに直面している:https://stackoverflow.com/questions/45614307/401-unauthorized-while-subscribing-to-push-notifications-with-exchange-service-a私は必要な許可を与えた紺碧のポータルで私のアプリが、まだ運がない。 – tavier

0

私は、ほぼ同じコードの実用的なソリューションを持っています。

認証コンテキストのURLは、https://login.microsoftonline.com/*tenant-id*であることがわかります。

はまた、私はnew ImpersonatedUserId(ConnectingIdType.SmtpAddress, email);

を使用しますが[email protected]がフルサインイン名である、またはそれだけで電子メールアドレスであることを確認していますか? enter image description here :それは私の側に取り組んでいる

関連する問題