C# 使用ADAL.js获取的令牌对Azure移动服务应用进行身份验证
我正在尝试根据Azure移动服务应用对HTML应用进行身份验证 设置 两个应用都使用AAD作为身份验证后端,因此两个应用都在Active Directory中注册了一个应用程序: Azure移动服务应用程序:C# 使用ADAL.js获取的令牌对Azure移动服务应用进行身份验证,c#,azure,mobile,azure-mobile-services,adal,C#,Azure,Mobile,Azure Mobile Services,Adal,我正在尝试根据Azure移动服务应用对HTML应用进行身份验证 设置 两个应用都使用AAD作为身份验证后端,因此两个应用都在Active Directory中注册了一个应用程序: Azure移动服务应用程序: 按照中所述进行配置 我编辑了清单以启用客户端流 在“Windows Azure Active Directory”的“其他应用程序权限”下启用“单一登录并读取用户配置文件” HTML应用程序: 在“对其他应用程序的权限”中,我添加了Azure移动服务应用程序,该应用程序具有委托权限“
- 按照中所述进行配置
- 我编辑了清单以启用客户端流
- 在“Windows Azure Active Directory”的“其他应用程序权限”下启用“单一登录并读取用户配置文件”
- 在“对其他应用程序的权限”中,我添加了Azure移动服务应用程序,该应用程序具有委托权限“访问”
adalAuthenticationServiceProvider.init(
{
// Config to specify endpoints and similar for your app
clientId: "<html app aad client id>",
redirectUri: "<html app redirect uri>",
endpoints: {
'<AMS app client id>': 'https://ampapp.azure-mobile.net/'
}
},
$httpProvider
);
adalAuthenticationServiceProvider.init(
{
//配置以指定应用程序的端点和类似项
客户ID:“,
重定向URI:“”,
端点:{
")
好的,让我们这样做:
// obtain token for Azure Mobile Service from Adal.js
var token = this.getAADToken(ZUMOAuthenticationProvider.Config().url);
$http({
method: 'POST',
url: ZUMOAuthenticationProvider.Config().url + 'login/aad',
data: JSON.stringify({
"access_token" : token
}),
headers: {
'X-ZUMO-APPLICATION': '<application key>'
}).
success(function (data, status, headers, config) {
alert(data);
}).
error(function (data, status, headers, config) {
alert(data);
});
//从Adal.js获取Azure移动服务的令牌
var token=this.getAADToken(ZUMOAuthenticationProvider.Config().url);
$http({
方法:“POST”,
url:ZUMOAuthenticationProvider.Config().url+'login/aad',
数据:JSON.stringify({
“访问令牌”:令牌
}),
标题:{
“X-ZUMO-APPLICATION”:
- 同样的问题,没有解决方案(即使在最后一条评论中链接的聊天日志中)
- 与上述作者相同,我检查了接受答案中提到的所有内容,但都不起作用
我还从新的Azure管理门户查看了新的Azure移动应用程序,但它们似乎使用相同的身份验证机制
那么,我如何才能让它工作呢?您可以使用AzureMobileServices客户端脚本使用已经获得的令牌进行登录:
您需要包括以下脚本:
然后使用ADAL.JS获取令牌后,您可以使用它登录并获取移动服务认证令牌:
var appUrl = 'https://foobar.azure-mobile.net'
, appKey = 'zumo key' // found on the dashboard of the mobile service
, client = new WindowsAzure.MobileServiceClient(appUrl, appKey);
// ...
var token = this.getAADToken(ZUMOAuthenticationProvider.Config().url);
client
.login('aad', { 'access_token': token })
.then(function() {
// client.currentUser.mobileServiceAuthenticationToken
});
然后,该令牌需要包含在后续移动服务API请求中:
var config = {
headers: {
'X-ZUMO-AUTH': client.currentUser.mobileServiceAuthenticationToken
}
}
$http
.get(appUrl + '/some/path', config)
.then(function (r) {
console.log(r);
});
帖子可能会返回401,因为AAD令牌的访问群体不正确。移动服务希望这是它的/login/AAD端点,但我怀疑你发送的令牌实际上作用域是你调用的网站。委托访问权限只是说你可以从网站获取令牌并将其转换为移动服务的令牌。它不会更改已颁发令牌本身的性质
因此,最好的建议是确保您正在登录移动服务受众,或者执行委托访问流。不幸的是,后者的示例似乎不太多
一种解决方法是将移动服务上的MS_AadAudience应用程序设置设置与您的网站设置相匹配。只有当网站和移动服务位于应用程序的同一逻辑安全边界内时,您才应该这样做。也就是说,任何可以登录到您的网站的人都可以在此时访问移动服务。总体而言更好的方法是获取移动服务的访问令牌。好的,我发现了我的错误:
endpoints: {
'<AMS app client id>': 'https://ampapp.azure-mobile.net/'
}
现在,您可以使用获取Azure移动服务身份验证令牌
var zumoAppID = 'https://zumodemoapp.azure-mobile.net/login/aad';
var zumoLoginUri = 'https://zumodemoapp.azure-mobile.net/login/aad';
var zumoTodoController = 'https://zumodemoapp.azure-mobile.net/tables/TodoItem';
// 1. acquire a oath token for our zumo app from azure ad via adal.js
adalAuthenticationService.acquireToken(zumoAppID).then(function (data) {
//2. we have the azure ad token, lets get a azure mobile service token
$http.post(zumoLoginUri,
JSON.stringify({
"access_token": data
})).
success(function (data, status, headers, config) {
//3. with the azure mobile service token we can authenticate our request
$http.get(zumoTodoController,
{
headers: {
'X-ZUMO-AUTH': data.authenticationToken
}
}).
success(function (data, status, headers, config) {
alert(data); //yay!
});
}).
error(function (data, status, headers, config) {
alert(data);
});
});
如评论中所述,我创建了一篇更详细的博客文章。如果您需要更多信息,请留下评论:).那么,你在AAD中设置了两个应用程序?你在这两个应用程序上都启用了客户端流吗?是的,在Azure AD中有两个应用程序,都启用了客户端流,因为它们都应该能够通过JS进行身份验证。重要提示:HTML应用程序应该被授予访问Azure移动服务的权限。你能给我发送一个JWT示例给/login/AAD端点吗可以将其与我的进行比较。检查JWT中有效负载的一个好资源是“”此网站具有解码您令牌内容的工具。我在验证使用Auth0返回的令牌时遇到问题。我将此令牌与使用pure azure时获得的令牌进行了比较,发现两者完全不同。AMS应用程序id uri是什么?我仍然无法使其正常工作。我99%确定我遵循了所有相同的步骤你正在这样做。这些其他问题是我输入的。你发送到端点的HTTP帖子是否包含身份验证头?AMS应用程序ID URI应该是https://.azure-mobile.net/login/aad 这是应在“配置”中设置的值“移动服务的AAD注册”选项卡。@Sven-准备好后,请用您的角度模块的位置更新这篇文章好吗?另外,如果您可以共享getAADToken()的定义,那就太好了。”。我的印象是,你只能从ADAL JS中获取ID令牌-如果知道如何获取访问令牌,那就太好了。@mattchenderson我将在Github上发布一个Angular模块,并在我的博客中写一篇关于它的文章,提供更多详细信息。我将在评论中发布URL,并用所有必要的细节更新我的答案。getAADToken使用ADAL.JS获取Azure移动服务的令牌。这就是我将HTML AAD应用配置为获取Azure移动应用的访问权限的原因。我将尝试发布更多内容,直到周五。@PilotBob希望此更新将帮助您解决问题
adalAuthenticationServiceProvider.init(
{
clientId:"54110492-4ae3-4c9f-9530-3101458d43fb",
redirectUri: "https://localhost:44304/",
endpoints: {
'https://zumodemoapp.azure-mobile.net/': 'https://zumodemoapp.azure-mobile.net/login/aad'
}
},
$httpProvider
);
var zumoAppID = 'https://zumodemoapp.azure-mobile.net/login/aad';
var zumoLoginUri = 'https://zumodemoapp.azure-mobile.net/login/aad';
var zumoTodoController = 'https://zumodemoapp.azure-mobile.net/tables/TodoItem';
// 1. acquire a oath token for our zumo app from azure ad via adal.js
adalAuthenticationService.acquireToken(zumoAppID).then(function (data) {
//2. we have the azure ad token, lets get a azure mobile service token
$http.post(zumoLoginUri,
JSON.stringify({
"access_token": data
})).
success(function (data, status, headers, config) {
//3. with the azure mobile service token we can authenticate our request
$http.get(zumoTodoController,
{
headers: {
'X-ZUMO-AUTH': data.authenticationToken
}
}).
success(function (data, status, headers, config) {
alert(data); //yay!
});
}).
error(function (data, status, headers, config) {
alert(data);
});
});