2017-08-10 214 views
0

如何在我的控制檯應用程序中針對Azure活動目錄對用戶進行身份驗證而無需重定向到登錄頁面?如何根據Azure Active Directory對用戶進行身份驗證

string tenantName = "---"; 
string authString = "https://login.microsoftonline.com/" + tenantName; 
AuthenticationContext authenticationContext = new AuthenticationContext(authString, false); 
// Config for OAuth client credentials 
string clientId = "---"; 
string key = "---"; 
ClientCredential clientCred = new ClientCredential(clientId, key); 
string resource = "https://pwsintsnapitazure.azurewebsites.net"; 
string token; 
AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(resource, clientCred).Result; 
token = authenticationResult.AccessToken; 
+0

當我嘗試上面的示例代碼時,我得到錯誤「ex = {」AADSTS70002:請求正文必須包含以下參數:'client_secret or client_assertion' –

+0

您的代碼正在使用客戶端憑證流。 ://www.cloudidentity.com/blog/2014/07/08/using-adal-net-to-authenticate-users-via-usernamepassword/)和[here](https://blogs.msdn.microsoft.com/wushuai/2016/09/25/resource-owner-password-credential-grant-in-azure-ad-oauth /)如何通過用戶名/密碼對用戶進行身份驗證[Here](https://github.com/ Azure-Samples/active-directory-dotnet-native-headless)是一個代碼示例。 –

+0

RequestMessage = {方法:POST,RequestUri:'https://login.microsoftonline.com/142d56e1-4ab5-4f5d-8140-d3db9fbf4cac/ oauth2/token?',版本:1.1,內容:System.Net.Http.StringContent,標題: { 接受:application/x-www-form-urlencoded 內容類型:application/x -... –

回答

0

獲取令牌的代碼(authenticationContext.AcquireTokenAsync(resource,clientCred))正在執行客戶端憑證流。通過此流程,應用程序向OAuth2令牌頒發端點呈現其客戶端憑證,並作爲迴應,獲取代表應用程序本身的訪問令牌,而無需任何用戶信息。

如果你想驗證對Azure中的Active Directory用戶和不顯示登錄頁面,你可以使用資源所有者流程,請點擊如何通過用戶名/密碼認證用戶herehere。請注意第一個鏈接中的Constraints & Limitations部分。

相關問題