Azure 我使用Msal从AAD获得了一个令牌,但可以';t使用获取的令牌获取用户配置文件。如何仅在节点后端验证令牌?
我使用以下配置从AAD请求令牌 app.module.ts文件:Azure 我使用Msal从AAD获得了一个令牌,但可以';t使用获取的令牌获取用户配置文件。如何仅在节点后端验证令牌?,azure,azure-active-directory,azure-ad-graph-api,msal,azure-authentication,Azure,Azure Active Directory,Azure Ad Graph Api,Msal,Azure Authentication,我使用以下配置从AAD请求令牌 app.module.ts文件: MsalModule.forRoot({ clientID: 'CLIENT_ID', authority: "https://login.microsoftonline.com/TENANT_ID", validateAuthority: true, cacheLocation: 'sessionStorage',
MsalModule.forRoot({
clientID: 'CLIENT_ID',
authority: "https://login.microsoftonline.com/TENANT_ID",
validateAuthority: true,
cacheLocation: 'sessionStorage',
postLogoutRedirectUri: 'http://localhost:4200/authorize/signin',
navigateToLoginRequestUrl: true,
popUp: true,
consentScopes: ['user.read', 'https://graph.microsoft.com']
}
它返回msal.idtoken、accesstoken和其他一些msal键值对。
现在,下面的代码用于通过粘贴获取的MSAL_IDTOKEN来获取用户的配置文件
const request = require('request');
const tok = 'MSAL_IDTOKEN HERE';
request.get({ url: "https://graph.microsoft.com/v1.0/me", headers: { "Authorization": "Bearer " + tok, "Content-type": "application/json" } }, function (err, response, body) {
if (err) {
console.log('err', err);
}
else
console.log(response.body);
})
现在,在节点上运行应用程序后,它通常会返回用户的配置文件(在解码令牌后找到),但现在它不会返回用户的配置文件。似乎您正在尝试从访问令牌读取用户配置文件 为此,您需要在azure portal上分配
配置文件
专用权限
请参见下面的屏幕截图:
注意:分配权限后,您可以检查您的令牌是否包含所需权限
令牌声明:
读取用户数据:
代码片段:
令牌类:
公共类AccessTokenClass
{
公共字符串标记\u类型{get;set;}
{get;set;}中的公共字符串过期
公共字符串资源{get;set;}
公共字符串作用域{get;set;}
公共字符串访问\u令牌{get;set;}
公共字符串刷新\u标记{get;set;}
}
令牌方法:
private异步任务GetTokenByROPCFormat()
{
字符串标记URL=$”https://login.microsoftonline.com/YourTenantIdOrName/oauth2/token";
var tokenRequest=newhttprequestmessage(HttpMethod.Post,tokenUrl);
tokenRequest.Content=newformurlencodedcontent(新字典
{
[“授权类型”]=“密码”,
[“客户id”]=“b603c7be-a866--e6921e61f925”,
[“客户机密”]=“Vxf1SluKbgu4PF0Nf3wE5oG”,
[“资源”]=”https://graph.microsoft.com",
[“用户名”]=”kironmemb@MyTenant.onmicrosoft.com",
[“密码”]=“@Mypassword”
});
动态json;
动态结果;
HttpClient=新的HttpClient();
var tokenResponse=wait client.sendaync(tokenRequest);
json=wait-tokenResponse.Content.ReadAsStringAsync();
结果=JsonConvert.DeserializeObject(json);
WriteLine(“您的刷新令牌=>{0}”,results.Refresh\u令牌);
//用于从API访问数据的新块
HttpClient newClient=新的HttpClient();
HttpRequestMessage请求=新建HttpRequestMessage(HttpMethod.Get)https://graph.microsoft.com/v1.0/me");
request.Headers.Authorization=新的AuthenticationHeaderValue(“承载者”,results.access\u令牌);
HttpResponseMessage response=Wait newClient.SendAsync(请求);
字符串输出=wait response.Content.ReadAsStringAsync();
返回输出;
}
我看到您在门户上有正确的配置
如果您使用的是MSAL.js,请给出如下代码:
this.app = new Msal.UserAgentApplication(
this.applicationConfig.clientID,
`https://login.microsoftonline.com/${AzureADName}/`,
() => {
// callback for login redirect
},
{
redirectUri
}
);
然后,您可以调用此来获取用户信息:
this.app.getUser();
或
您必须提供版本信息才能确定,因为API已更改。Get User Profile仅适用于
msal:“^0.2.4”
,而不适用于当前版本1.1。您在azure portal上分配权限了吗?是的,它已添加。@AniketSingh您能帮我获取“const tok='msal_IDTOKEN HERE';“使用msal。是的,在门户中添加了配置文件权限。它仍然不会返回配置文件。仅供参考,当我在jwt.ms中粘贴令牌时,它会解码并提供与我需要的几乎相同的配置文件。@AniketSingh您是否在jwt上解码了您的令牌?它是否包含该权限?“aud”:“CLIENT_ID”,“iss”:“iat”:“nbf”:“exp”:1559207087,“aio”:“名称”:“Aniket Singh”,“nonce”:“oid”:“首选用户名”:”Aniket@ssrx.com这是我收到的JSON。我故意删除了这些值并给它们添加了别名。您能告诉我您是如何生成令牌的吗?URL和rest?非常感谢。
this.app.getAccount();