我在 github 示例下尝试了 Azure AD 身份验证,并成功获取了具有正确回调 URL 的访问令牌。
根据我在https://apps.dev.microsoft.com中创建的应用程序修改和配置 Web.config 密钥
<add key="ActiveDirectory.Mode" value="v1" />
<add key="ActiveDirectory.ResourceId" value="https://graph.microsoft.com" />
<add key="ActiveDirectory.EndpointUrl" value="https://login.microsoftonline.com" />
<add key="ActiveDirectory.Tenant" value="xyz-xyz" />
<add key="ActiveDirectory.ClientId" value="xyz-xyz" />
<add key="ActiveDirectory.ClientSecret" value="xyz-xyz" />
<add key="ActiveDirectory.RedirectUrl" value="http://localhost:3979/api/Callback" />
<add key="ActiveDirectory.Scopes" value="Calendars.Read,Calendars.ReadWrite,Contacts.Read,Contacts.ReadWrite,Mail.Read,Mail.ReadWrite,User.Read" />
我已经检查了收到jwt.ms的访问令牌。它是一个有效的令牌,其范围与我在 web.config 中定义的完全一致。
我可以通过传递令牌从 API 获取用户配置文件数据,但是在尝试调用用户的“联系人”时,我遇到了错误。
{
"error": {
"code": "ResourceNotFound",
"message": "Resource could not be discovered.",
"innerError": {
"request-id": "6f0f3ec9-76c9-4662-ac25-0bc73f887268",
"date": "2019-03-02T17:23:35"
}
}
}
代码调用 Contact API 调用。
//Get Logged in user contacts
public async Task<IUserContactsCollectionPage> GetMyContactsAsync()
{
var graphClient = GetAuthenticatedClient();
IUserContactsCollectionPage contacts = await graphClient.Me.Contacts.Request().GetAsync();
return contacts;
}
仅供参考:在模拟器中显示的登录卡授权示例 URL 如下所示
https://login.microsoftonline.com/a76b43b0-3088-4c56-ba0d-01f317b1f18c/oauth2/authorize?resource=https:%2F%2Fgraph.microsoft.com&client_id=XYZ&response_type=code&haschrome=1&redirect_uri=http:%2F%2Flocalhost:3979%2Fapi%2FCallback&x-client-SKU=PCL.Desktop&x-client-Ver=3.13.9.1126&x-client-CPU=x64&x-client-OS=Microsoft+Windows+NT+10.0.16299.0&state=<stateID>