Comment puis-je obtenir un jeton d'application pour interroger SharePoint avec les informations d'identification de l'application (= sans usurpation d'identité de l'utilisateur) à l'aide d'Azure AD?Accès d'application à SharePoint Online à l'aide d'Azure AD Token
Le code suivant fonctionne parfaitement pour la recherche de données en tant qu'utilisateur mais nous avons besoin d'aller chercher des informations sans usurpation d'identité comme la liste de tous les sites de la collection indépendamment des autorisations d'utilisateur, etc.
Exception levée:
Une exception de type 'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException' produite dans mscorlib.dll mais n'a pas été traitée dans le code utilisateur
informations complémentaires: AADS TS70001: Application avec identifiant 'xxx' n'a pas été trouvé dans le répertoire sharepoint.com
code pour obtenir jeton:
internal static async Task<string> GetSharePointAccessToken(string url, string userAccessTokenForImpersonation)
{
string clientID = @"<not posted on stack overflow>";
string clientSecret = @"<not posted on stack overflow>";
var appCred = new ClientCredential(clientID, clientSecret);
var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext("https://login.windows.net/common");
// Use user assetion if provided, otherwise use principal account
AuthenticationResult authResult = null;
if (string.IsNullOrEmpty(userAccessTokenForImpersonation))
{
authResult = await authContext.AcquireTokenAsync(new Uri(url).GetLeftPart(UriPartial.Authority), appCred);
}
else
{
authResult = await authContext.AcquireTokenAsync(new Uri(url).GetLeftPart(UriPartial.Authority), appCred, new UserAssertion(userAccessTokenForImpersonation));
}
return authResult.AccessToken;
}
Code d'essai:
// Auth token from Bearer https://xxx.azurewebsites.net/.auth/me
string authHeader = @"<valid jwt bearer token from azure auth>";
var sharePointUrl = @"https://xxx.sharepoint.com/sites/testsite/";
string sharePrincipalToken = await GetSharePointAccessToken(sharePointUrl, null); // <-- doesn't work
string sharePointUserToken = await GetSharePointAccessToken(sharePointUrl, authHeader); // <-- works
Autorisations dans Azure AD: