2016-07-29 130 views
1

我正在嘗試構建一個允許客戶與我約會的(C#)Web應用程序。 我想讓網絡應用程序能夠讀取和添加條目到我的Outlook日曆。 許多用戶將使用網絡應用程序,但該網絡應用程序只能訪問一個Outlook日曆 - 我的。 我已經能夠工作的所有示例都涉及到Web應用程序用戶交互認證 - 但我的用戶不知道我的密碼。 我想在Web應用程序中硬編碼我的用戶名/電子郵件地址和密碼。使用Office Outlook API和硬編碼用戶名和密碼

當試圖獲取令牌我得到一個錯誤:

Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException: 
AADSTS65001: The user or administrator has not consented to use the application. 
Send an interactive authorization request for this user and resource. 

我不是租戶的管理員。有什麼方法可以讓我的工作不需要管理員參與? 會使用某種證書而不是用戶名和密碼作爲用戶憑證幫助嗎?

我的代碼(目前在一個簡單的C#控制檯應用程序)如下:

UserCredential uc = new UserCredential(MyUsername, MyPassword); 
var AuthContext = new AuthenticationContext("https://login.windows.net/Common"); 

// this doesn't work unless an unexpired token already exists 
ar = AuthContext.AcquireToken("https://outlook.office365.com/", MyClientId, uc); 

// this does work, but requires the app user to know the password 
ar = AuthContext.AcquireToken("https://outlook.office365.com/", MyClientId, new Uri(MyReturnURI)); 

回答

2

要啓用使用用戶名和密碼,直接要求道理,我們需要同意使用的應用程序。

我們可以使用OAuth 2.0授權碼授權流程授予用戶同意。下面是一個示例使用ADAL認證庫(3.13.1.846)獲得委託令牌:

static string authority= "https://login.microsoftonline.com/common"; 
public static string GetDeligateToken(string resource, string clientId,string redirectURL) 
    { 
     AuthenticationContext authContext = new AuthenticationContext(authority); 
     AuthenticationResult authResult= authContext.AcquireTokenAsync(resource, clientId,new Uri(redirectURL), new PlatformParameters(PromptBehavior.Auto)).Result; 
     return authResult.AccessToken; 
    } 

我們同意該應用程序後,現在我們可以使用代碼在你的崗位上獲得令牌。

+0

對不起,我不明白。如果我使用您提供的代碼,那麼它會將我發送到網頁進行登錄(並且如果我提供了我的用戶名和密碼,則它會返回一個令牌)。但是,該代碼何時意味着要執行並由誰來執行?我試圖實現的Web應用程序將被不知道我的用戶名和密碼的其他用戶使用。我的意思是執行提前提供的代碼,檢索令牌並在我的Web應用程序中將其作爲字符串進行硬編碼?我認爲這些代幣會在一個小時左右後過期? – Wayne

+0

在文章中運行代碼後,請使用您想要在原始文章中使用的帳戶登錄該頁面。然後你可以看到一個同意頁面,並且在你獲得訪問令牌後,你的原始帖子中的問題應該是固定的(你已經**同意使用應用程序**)。 –

+0

謝謝,這似乎工作。所以這個同意永遠持續下去? – Wayne