簡體   English   中英

如何使用Web應用程序/ WebAPI驗證Azure AD中的用戶憑據

[英]How to validate user credentials in Azure AD with Web application / WebAPI

我有一個Web應用程序。 在主頁中,用戶將輸入憑據,系統應針對Azure AD進行驗證並進一步進行操作。

當我使用本機應用程序並使用UserCredentials ,它將驗證用戶,但是如果我對WebAPI使用相同的方法,則會引發異常

請求正文必須包含以下參數:'client_secret或client_assertion'

當我使用帶clientCredentials的WebAPI clientCredentials ,它會生成accessToken,它不會驗證用戶憑據。 我還嘗試在后續調用中將憑據作為httpclient標頭的一部分傳遞,盡管憑據錯誤,它仍可以正常工作。

string AzureADSTSURL = "https://login.windows.net/{0}/oauth2/token?api-version=1.0";
string GraphPrincipalId = "https://graph.windows.net";

string userid = "userid";
string password = "pass";

string tenantId = "axxx";   //  webapi
string clientId = "bxxx";
string clientSecret = "cxxx";
string authString = String.Format(AzureADSTSURL, tenantId);

var context = new AuthenticationContext(authString);

UserCredential userCredentials = new UserCredential(userid, password);
AuthenticationResult authenticationResult = context.AcquireToken(GraphPrincipalId.ToString(), clientId, userCredentials); // this works only if the clientId corresponds to a native app

ClientCredential clientCredential = new ClientCredential(clientId, clientSecret);
AuthenticationResult result = context.AcquireToken(GraphPrincipalId, clientCredential);


HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(result.AccessToken, Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(userid + ':' + password)));

httpClient.GetAsync("http://localhost:11455/Login.aspx");

有沒有一種方法可以在不使用本機應用程序的情況下驗證憑據? 我認為,Graph API不是正確的選擇。

我試圖做同樣的事情,並遇到了同樣的錯誤:

請求正文必須包含以下參數:'client_secret或client_assertion'

我敲了一下頭,然后在Twitter上打了AzureSupport。

事實證明,只有在將Azure AD應用程序設置為本Native Client Application程序時,才支持這種身份驗證。 如果將其設置為Web Application則會出現該錯誤,因為在Azure AD中訪問Web應用程序的唯一方法是通過客戶端ID +機密。

您可以在一個AD上擁有多個應用程序,因此只需將第二個應用程序設置為本地客戶端即可對該目錄中的相同用戶進行身份驗證。

您當然可以使用WebAPI。 設置方法如下:

如果使用支持ASP.NET MVC的Azure Web Apps,則可以使用Azure Active Directory身份驗證機制。 這是描述其設置方法的博客文章: https : //azure.microsoft.com/zh-cn/documentation/articles/app-service-mobile-how-to-configure-active-directory-authentication/

完成后,將為您的應用程序啟用身份驗證,並且您可以在門戶中配置AAD應用程序。 有關更多詳細信息,請參見此博客文章: http : //blogs.technet.com/b/ad/archive/2014/12/18/azure-active-directory-now-with-group-claims-and-application-roles。 aspx

這是顯示如何從Web應用程序讀取AAD組聲明的示例: https : //github.com/Azure-Samples/active-directory-dotnet-webapp-groupclaims

獲得令牌后,就可以調用此示例顯示的Web API: https : //github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-openidconnect

這里有一個很好的AAD示例列表: https : //azure.microsoft.com/zh-cn/documentation/articles/active-directory-authentication-scenarios/

簡短答案:否

我認為這篇文章是為什么的權威答案。

沒有網站/機密客戶
這不是ADAL限制,而是AAD設置。 您只能使用來自本機客戶端的流。 機密客戶端(例如網站)不能使用直接用戶憑據。

daccess-ods.un.org daccess-ods.un.org直接使用用戶名和密碼是浮士德條約的一部分–您因直接性而付出的代價是它受到許多限制,並且依賴於該解決方案的靈活性降低。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM