【问题标题】:How to authenticate user against Azure Active Directory如何针对 Azure Active Directory 对用户进行身份验证
【发布时间】:2017-08-10 05:49:43
【问题描述】:

如何在我的控制台应用程序中针对 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;

【问题讨论】:

  • 当我尝试上面的示例代码时,我得到了错误“ex = {”AADSTS70002:请求正文必须包含以下参数:'client_secret or client_assertion"
  • 您的代码正在使用客户端凭据流。请点击herehere 了解如何通过用户名/密码验证用户。 Here 是一个代码示例。
  • RequestMessage = {方法:POST,RequestUri:'login.microsoftonline.com/142d56e1-4ab5-4f5d-8140-d3db9fbf4cac/…?',版本:1.1,内容:System.Net.Http.StringContent,标头:{接受:应用程序/x-www-form -urlencoded Content-Type: application/x-...
  • 在您的新帖子中查看我的回复:*.com/questions/45609432/…

标签: c# azure azure-active-directory


【解决方案1】:

使用您的代码 (authenticationContext.AcquireTokenAsync(resource, clientCred)) 获取令牌正在执行客户端凭据流。通过此流程,应用程序将其客户端凭据提供给 OAuth2 令牌颁发端点,并作为回报获得代表应用程序本身的访问令牌,而无需任何用户信息。

如果您想针对 Azure Active Directory 对用户进行身份验证而不显示登录页面,您可以使用资源所有者流程,请单击 herehere 了解如何通过用户名/密码对用户进行身份验证。请注意第一个链接中的Constraints & Limitations部分。

【讨论】: