简体   繁体   English

使用Azure AD身份验证(B2C)显示Core MVC的头像

[英]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.

相关问题 如何在 asp net core mvc 应用程序中从我的 Azure AD B2C 获取用户列表? - How to get list of Users from my Azure AD B2C in asp net core mvc app? Azure B2C AD / dotnetcore 2.0 MVC 应用程序未授权 - Azure B2C AD / dotnetcore 2.0 MVC app not authorising ASP.NET Core MVC 6 Azure B2C Active Directory身份验证问题 - ASP.NET Core MVC 6 Azure B2C Active Directory Authentication issue Azure B2C保持在MVC 6 .Net Core 2中的签名 - Azure B2C Keep signed in MVC 6 .Net Core 2 Azure AD B2C 和 Microsoft Identity Web - 使用多个策略登录 (.net Core 3.1) - Azure AD B2C & Microsoft Identity Web - Sign In with multiple policies (.net Core 3.1) .NET Core 使用 Azure B2C 身份验证,登录后查找 @User.Identity.UserID 等用户属性 - .NET Core using Azure B2C Authentication, looking for the User Attributes such as @User.Identity.UserID after logged in 结合Azure AD(b2c)自定义ClaimsIdentity - Combining Azure AD (b2c) custom ClaimsIdentity Azure Web应用程序计划中的Azure AD B2C返回URL - Azure AD B2C return url in azure web app plan 我们可以申请 ASP.NET Core 3.1 MVC Azure AD 认证使用 HTTP - Can we apply ASP.NET Core 3.1 MVC Azure AD authentication using HTTP 使用 dotnet Core Web MVC 应用程序找不到 Azure AD 身份验证的质询和身份验证方案 - Challenge and Authentication Schemes not found for Azure AD Authentication with dotnet Core Web MVC app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM