我使用以下配置从AAD请求令牌。
app.module.ts文件:
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);
})
现在,在Node上运行该应用程序之后,它曾经返回用户的配置文件,这是在解码令牌后发现的,但现在没有。
答案 0 :(得分:0)
您似乎正在尝试从访问令牌中读取用户个人资料。
为此,您需要在Azure门户上分配profile
专用权限。
请参见以下屏幕截图:
注意:分配权限后,您可以在
https://jwt.io/
上检查令牌是否包含必需的权限。
令牌声明:
读取用户数据:
代码段:
令牌类别:
public class AccessTokenClass
{
public string token_type { get; set; }
public string expires_in { get; set; }
public string resource { get; set; }
public string scope { get; set; }
public string access_token { get; set; }
public string refresh_token { get; set; }
}
令牌方法:
private async Task<string> GetTokenByROPCFormat()
{
string tokenUrl = $"https://login.microsoftonline.com/YourTenantIdOrName/oauth2/token";
// string tokenUrl = $"https://login.microsoftonline.com/hanxia.onmicrosoft.com/oauth2/token";
var tokenRequest = new HttpRequestMessage(HttpMethod.Post, tokenUrl);
tokenRequest.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
["grant_type"] = "password",
["client_id"] = "b603c7be-a866--e6921e61f925",
["client_secret"] = "Vxf1SluKbgu4PF0Nf3wE5oG",
["resource"] = "https://graph.microsoft.com",
["username"] = "kironmemb@MyTenant.onmicrosoft.com",
["password"] = "@Mypassword"
});
dynamic json;
dynamic results;
HttpClient client = new HttpClient();
var tokenResponse = await client.SendAsync(tokenRequest);
json = await tokenResponse.Content.ReadAsStringAsync();
results = JsonConvert.DeserializeObject<AccessTokenClass>(json);
Console.WriteLine("Your Refresh Token=>{0}", results.refresh_token);
// New Block For Accessing Data from API
HttpClient newClient = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", results.access_token);
HttpResponseMessage response = await newClient.SendAsync(request);
string output = await response.Content.ReadAsStringAsync();
return output;
}
答案 1 :(得分:0)
我看到您在门户网站上具有正确的配置。
如果您使用的是MSAL.js,请提供以下代码:
this.app = new Msal.UserAgentApplication(
this.applicationConfig.clientID,
`https://login.microsoftonline.com/${AzureADName}/`,
() => {
// callback for login redirect
},
{
redirectUri
}
);
然后您将调用它来获取用户信息:
this.app.getUser();
或
this.app.getAccount();
随着API的更改,您必须提供版本信息才能确定。
答案 2 :(得分:0)
获取用户个人资料仅适用于msal": "^0.2.4"
,不适用于当前版本1.1。