[英]Showing avatar for Core MVC using Azure AD Authentication (B2C)
I have a ASP.NET Core MVC application that is integrated with our Azure AD. 我有一个与我们的Azure AD集成的ASP.NET Core MVC应用程序。 In the default template that is created for a new MVC project, it shows the name of the logged in user, but I'd like to show the avatar as well (image associated with the user like Azure does when logging into the portal).
在为新的MVC项目创建的默认模板中,它显示了登录用户的名称,但是我也想显示化身(与用户关联的图像就像Azure在登录门户时一样)。
Can this be done? 能做到吗?
You can use Microsoft Graph API to get the photo of the current user : 您可以使用Microsoft Graph API获取当前用户的照片:
GET https://graph.microsoft.com/v1.0/me/photo/$value
Reference : https://docs.microsoft.com/en-us/graph/api/profilephoto-get?view=graph-rest-1.0 参考: https : //docs.microsoft.com/zh-cn/graph/api/profilephoto-get?view=graph-rest-1.0
To use Microsoft Graph API , you need to get access token in your MVC application using MSAL : 要使用Microsoft Graph API,您需要使用MSAL在MVC应用程序中获取访问令牌:
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext context)
{
var clientCredential = new ClientCredential(context.Options.ClientSecret);
var userId = context.Principal.FindFirst(ClaimTypes.NameIdentifier).Value;
var tokenCache = new SessionTokenCache(context.HttpContext, userId);
var confidentialClientApplication = new ConfidentialClientApplication(
context.Options.ClientId,
context.Options.Authority,
_options.RedirectUri,
clientCredential,
tokenCache.GetInstance(),
null);
try
{
var authenticationResult = await confidentialClientApplication.AcquireTokenByAuthorizationCodeAsync(context.ProtocolMessage.Code, _options.ApiScopes.Split(' '));
context.HandleCodeRedemption(authenticationResult.AccessToken, authenticationResult.IdToken);
}
catch (Exception ex)
{
// TODO: Handle
throw;
}
}
please refer to code sample : 请参考代码示例:
https://github.com/chrispadgettlivecom/WebApp-OpenIDConnect-DotNetCore21/tree/master/WebApp-OpenIDConnect-DotNetCore21 https://github.com/chrispadgettlivecom/WebApp-OpenIDConnect-DotNetCore21/tree/master/WebApp-OpenIDConnect-DotNetCore21
Or using the Microsoft Graph Client Library for .NET (SDK) 或使用Microsoft Graph Client Library for .NET(SDK)
https://github.com/microsoftgraph/aspnetcore-connect-sample https://github.com/microsoftgraph/aspnetcore-connect-sample
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.