繁体   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