Hi @yuvraj khanna , you might try my codes below as I noticed that you are using new ClientSecretCredential. This means you are using client credential flow which requires Application type of API permission. Client credential flow doesn't require a user to sign in first. The codes below allow the application to get profile photo of any user in your tenant base on the user id.
using Microsoft.Graph;
using Azure.Identity;
public async Task<IActionResult> clientAsync() {
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "tenant_id";
var clientId = "client_id";
var clientSecret = "client_Secret";
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var users = await graphClient.Users.GetAsync();
var photo = await graphClient.Users["user_id/user_principle_name"].Photo.Content.GetAsync();
return File(photo, "image/jpeg");
}
The error does not contain a definition for 'Request'... you faced is due to you are using codes for SDK v4.X, but you are using V5.56.0.
========================================
Asking user to sign-in first and get his/her profile photo requires the application to integrate Azure AD authentication, then we can use the Graph SDK for .net to call Graph API. We should have codes below in Program.cs
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
.EnableTokenAcquisitionToCallDownstreamApi()
.AddMicrosoftGraph(builder.Configuration.GetSection("DownstreamApi"))
.AddInMemoryTokenCaches();
In appsetting.json, my configuration is like this.
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"ClientId": "client_id",
"ClientSecret": "client_secret",
"Domain": "tenant_id",
"TenantId": "tenant_id",
"CallbackPath": "/signin-oidc"
},
"DownstreamApi": {
"BaseUrl": "https://graph.microsoft.com/v1.0",
"Scopes": "User.Read.All"
},
Then in the controller, we should inject GraphClient:
private GraphServiceClient _graphServiceClient;
public HomeController(GraphServiceClient graphServiceClient)
{
_graphServiceClient = graphServiceClient;
}
public async Task<IActionResult> IndexAsync()
{
var me = await _graphServiceClient.Me.GetAsync();
var photo = await graphClient.Me.Photo.Content.GetAsync();
return File(photo, "image/jpeg");
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Tiny