2016-04-11 54 views
2

我有一個網站(應用程序)在Azure Active Directory中註冊。我需要我的Web測試在開始時對自己進行身份驗證(使用預先存在的測試用戶),以便獲得用於測試的身份驗證令牌以訪問受保護的API。以編程方式登錄到Azure Active Directory

在C#中完成此操作的最佳方法是什麼?

回答

2

你可以嘗試做這樣的事情如下:

var authContext = new AuthenticationContext("https://login.microsoftonline.com/{tenantid}") 
    UserCredential userCredential = new UserCredential(userName, password); 
    AuthenticationResult authResult = authContext.AcquireToken("https://graph.windows.net/", clientId, userCredential); 

userNamepassword是用戶名和測試用戶的密碼。 authResult有一個名爲AccessToken的成員可以傳遞給您想要測試的方法。

+0

當我從CLI測試應用程序嘗試這個時,我得到一個「請求主體必須包含以下參數:'client_secret或client_assertion'異常。 – Cuthbert

+0

基於http://stackoverflow.com/questions/26846357/adal-the-request-body-must-contain-the-follow-parameter-client-secret,我相信你只能使用我提到的方法來處理本機應用程序而不是網絡應用程序。 –