Je tente d'extraire l'emplacement du bureau pour un utilisateur d'annuaire Azure Active authentifié via Microsoft Graph mais je reçois toujours une réponse 403 Forbidden.Interdit 403 lors de l'accès à Microsoft Graph à partir d'une application principale dotnet
Je suis en mesure d'authentifier et je peux générer un jeton d'accès, mais le code d'état de réponse HTTP est toujours 403.
Voici un code que je travaille avec mais j'ai le sentiment que peut-être dû à la configuration ou des autorisations alors s'il vous plaît laissez-moi savoir quelles informations supplémentaires vous auriez besoin.
public class AccountService : IAccountService
{
private readonly AzureAd _adSettings;
public AccountService(IOptions<AzureAd> adSettings)
{
_adSettings = adSettings.Value;
}
public async Task<string> GetStoreIdFromUser(string userId)
{
var storeId = string.Empty;
string accessToken = await GetBearerAccesToken();
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Get, GetUserUrl(userId)))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
using (var response = await client.SendAsync(request))
{
if (response.StatusCode == HttpStatusCode.OK)
{
var json = JObject.Parse(await response.Content.ReadAsStringAsync());
storeId = json?["physicalDeliveryOfficeName"]?.ToString();
}
}
}
}
return storeId;
}
#region private methods
private string GetUserUrl(string userPrincipalName)
{
return string.Format("https://graph.windows.net/{0}/users/{1}?{2}", _adSettings.TenantId, userPrincipalName, "api-version=1.6");
}
private async Task<string> GetBearerAccesToken()
{
string result = string.Empty;
// Get OAuth token using client credentials
string authString = "https://login.microsoftonline.com/" + _adSettings.TenantId;
AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
// Config for OAuth client credentials
ClientCredential clientCred = new ClientCredential(_adSettings.ClientId, _adSettings.AppKey);
string resource = "https://graph.windows.net";
AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(resource, clientCred);
result = authenticationResult.AccessToken;
return result;
}
#endregion
}
Êtes-vous essayez d'accéder à Microsoft Graph ou Azure AD Graph? –
Devrait être Microsoft Graph. J'ai ajouté des autorisations pour cela dans mon inscription à l'application AD. –
403 signifie "Je sais qui vous êtes, mais vous ne pouvez pas accéder à cette chose." Vérifiez le jeton sur https://jwt.io et constatez que la revendication d'audience (aud) est l'URI de ressource Microsoft Graph ('https: // graph.microsoft.com') et que les rôles nécessaires sont dans le jeton. . – juunas