简体   繁体   English

Azure AD B2C - 通过 REST 获取登录用户的缩略图 API

[英]Azure AD B2C - Get thumbnail image for logged in user via REST API

I'm trying to get thumbnail image/profile picture for my logged in Azure AD B2C user.我正在尝试获取我登录的 Azure AD B2C 用户的缩略图/个人资料图片。

I have been reading up but so far nothing has helped.我一直在阅读,但到目前为止没有任何帮助。

This is for a Azure Active Directory Graph Client but I would like to get it via Rest for the currently logged in user.这是一个 Azure Active Directory 图形客户端,但我想通过 Rest 为当前登录的用户获取它。 I would not like to require an administrator account to get it.我不想要求管理员帐户来获取它。

https://social.msdn.microsoft.com/Forums/en-US/305156f6-d6e9-4134-87b6-043c9c64ac8c/getting-a-profile-picture-under-azure-ad-b2c?forum=WindowsAzureAD https://social.msdn.microsoft.com/Forums/en-US/305156f6-d6e9-4134-87b6-043c9c64ac8c/getting-a-profile-picture-under-azure-ad-b2c?forum=WindowsAzureAD

https://stackoverflow.com/a/41056923/3850405 https://stackoverflow.com/a/41056923/3850405

What looked really promising was this endpoint but given that it is graph.windows.net I'm not sure it will work given that my permissions later is set for graph.microsoft.com .看起来真正有前途的是这个端点,但鉴于它是graph.windows.net我不确定它是否会起作用,因为我稍后为graph.microsoft.com设置了权限。

https://graph.windows.net/[tenant]/users/[objectid]/thumbnailPhoto?api-version=1.6

https://stackoverflow.com/a/55943064/3850405 https://stackoverflow.com/a/55943064/3850405

Preferably I would like to call an endpoint like this to get the currently logged in users picture but the above endpoint is of course OK.最好我想调用这样的端点来获取当前登录的用户图片,但上面的端点当然可以。

https://graph.microsoft.com/v1.0/me/photo/$value

https://learn.microsoft.com/en-us/graph/api/profilephoto-get?view=graph-rest-1.0#request However to call this https://learn.microsoft.com/en-us/graph/api/profilephoto-get?view=graph-rest-1.0#request但是调用这个

Both of these methods requires a valid bearer token.这两种方法都需要有效的不记名令牌。

What I have done so for is accessing my Azure AD B2C and then App registrations .我这样做是为了访问我的Azure AD B2C ,然后是App registrations Select my application and then API permissions . Select 我的应用程序然后API permissions Select Add a permission . Select Add a permission

Click on Microsoft Graph and then add profile as the picture below:点击Microsoft Graph然后添加profile如下图:

在此处输入图像描述

If I follow the guide for Get access on behalf of a user within the Microsoft Graph documentation this will lead to Microsofts login page like this:如果我遵循 Microsoft Graph 文档Get access on behalf of a user的指南,这将导致 Microsoft 的登录页面如下所示:

在此处输入图像描述

https://learn.microsoft.com/en-us/graph/auth-v2-user https://learn.microsoft.com/en-us/graph/auth-v2-user

If I do login I will get an error as expected:如果我登录,我会收到预期的错误:

在此处输入图像描述

Sign in Sorry, but we're having trouble signing you in.登录 抱歉,我们无法让您登录。

AADSTS50020: User account 'oscar.andersson@' from identity provider '' does not exist in tenant '' and cannot access the application ''() in that tenant. AADSTS50020:来自身份提供者“”的用户帐户“oscar.andersson@”在租户“”中不存在,无法访问该租户中的应用程序“()”。 The account needs to be added as an external user in the tenant first.需要先在租户中将账号添加为外部用户。 Sign out and sign in again with a different Azure Active Directory user account.注销并使用不同的 Azure Active Directory 用户帐户再次登录。

When I log in to my application normally I get a screen that looks like this.当我正常登录到我的应用程序时,我会看到如下所示的屏幕。 Works perfectly and connects to a instance with a domain name like: https://<myadb2c>.b2clogin.com/tfp/ .完美运行并连接到具有以下域名的实例: https://<myadb2c>.b2clogin.com/tfp/

在此处输入图像描述

Source:资源:

https://stackoverflow.com/a/61105325/3850405 https://stackoverflow.com/a/61105325/3850405

If I do login with my administrator account for Get access on behalf of a user I do get a code returned without a consent experience.如果我确实使用我的管理员帐户登录以Get access on behalf of a user我确实会在未经同意的情况下返回代码。

https://learn.microsoft.com/en-us/graph/auth-v2-user?context=graph/api/1.0#consent-experience https://learn.microsoft.com/en-us/graph/auth-v2-user?context=graph/api/1.0#consent-experience

However when I make the POST request to get a token I get the following response:但是,当我发出 POST 请求以获取令牌时,我得到以下响应:

https://learn.microsoft.com/en-us/graph/auth-v2-user?context=graph/api/1.0#token-request https://learn.microsoft.com/en-us/graph/auth-v2-user?context=graph/api/1.0#token-request

{
    "error": "invalid_grant",
    "error_description": "AADSTS65001: The user or administrator has not consented to use the application with ID '' named ''. Send an interactive authorization request for this user and resource.\r\nTrace ID: \r\nCorrelation ID: \r\nTimestamp: 2020-08-21 12:23:13Z",
    "error_codes": [
        65001
    ],
    "timestamp": "2020-08-21 12:23:13Z",
    "trace_id": "",
    "correlation_id": "",
    "suberror": "consent_required"
}

According to the documentation:根据文档:

The Microsoft identity platform v2.0 endpoint will also ensure that the user has consented to the permissions indicated in the scope query parameter. Microsoft 标识平台 v2.0 终结点还将确保用户已同意 scope 查询参数中指示的权限。 If the user has not consented to any of those permissions and if an administrator has not previously consented on behalf of all users in the organization, they will be asked to consent to the required permissions.如果用户未同意任何这些权限,并且如果管理员之前未代表组织中的所有用户同意,则将要求他们同意所需的权限。

Admin consent has been given for the permissions:已获得管理员同意以下权限:

在此处输入图像描述

If I change the working login to use scope https://graph.microsoft.com/profile instead of https://<tenant-name>.onmicrosoft.com/<client-id>/read I will get a code but not a token.如果我将工作登录更改为使用 scope https://graph.microsoft.com/profile而不是https://<tenant-name>.onmicrosoft.com/<client-id>/read我会得到一个代码但不是一个令牌。 Error:错误:

{"error":"invalid_grant","error_description":"AADB2C90205: This application does not have sufficient permissions against this web resource to perform the operation.\r\nCorrelation ID: \r\nTimestamp: 2020-08-21 13:36:44Z\r\n"} {"error":"invalid_grant","error_description":"AADB2C90205:此应用程序对此 web 资源没有足够的权限来执行操作。\r\n相关 ID:\r\n时间戳:2020-08-21 13: 36:44Z\r\n"}

If I try to call https://graph.microsoft.com/profile with the token from scope https://<tenant-name>.onmicrosoft.com/<client-id>/read I will get the error:如果我尝试使用来自 scope 的令牌调用https://graph.microsoft.com/profile https://<tenant-name>.onmicrosoft.com/<client-id>/read我将收到错误消息:

{
  "error": {
    "code": "InvalidAuthenticationToken",
    "message": "Access token validation failure.",
    "innerError": {
      "date": "2020-08-21T14:02:14",
      "request-id": ""
    }
  }
}

I really did not expect it to be this hard to get the profile picture out.真没想到头像这么难弄出来。 Getting the given name and surname is no problem since this information is returned from the Sign up and sign in (Standard) User flow as claims.获取给定的名字和姓氏没有问题,因为此信息作为声明从Sign up and sign in (Standard) User flow返回。

在此处输入图像描述

I have read how I could Manage Azure AD B2C with Microsoft Graph but this is not what I wan't.我已经阅读了如何使用 Microsoft Graph 管理 Azure AD B2C,但这不是我想要的。 I don't wan't to create a new App registrations simply because I wan't to fetch an already logged in users thumbnail.我不想仅仅因为我不想获取已经登录的用户缩略图而创建新的应用程序注册。

https://learn.microsoft.com/en-us/azure/active-directory-b2c/microsoft-graph-get-started?tabs=app-reg-ga https://learn.microsoft.com/en-us/azure/active-directory-b2c/microsoft-graph-get-started?tabs=app-reg-ga

The photo endpoint is not supported in Azure B2C. Azure B2C 不支持照片端点 You might create a custom API to store and retrieve them using jQuery , UI customization and/or Custom Policies and REST technical profile .您可以创建自定义 API 以使用jQueryUI 自定义和/或自定义策略REST 技术配置文件来存储和检索它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM