c# - 为什么 AuthenticationResult B2C 认证中 accessToken 为空?

标签 c# api authentication xamarin.forms azure-ad-b2c

我正在开发一个 xamarin.form 项目。我根据此示例添加 B2C 身份验证: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/authentication/azure-ad-b2c 我完全复制和粘贴,并没有改变方法。现在它可以工作了,但问题出在 AuthenticationResult ar 参数中,访问 token 为空,并且到期日期也是错误的。我无法解决它。 I attached the image of AuthenticationResult content 如果您需要更多信息,请告诉我。

最佳答案

您的范围值有误。访问 token 用于访问 protected 资源,例如 Web api。范围是管理对这些 protected 资源的权限的方式。 Web api 使用范围来实现基于范围的访问控制。

例如,要为具有 https://contoso.onmicrosoft.com/api 的 App ID URI 的 API 指定 read 的范围值,则范围将是 https://contoso.onmicrosoft.com/api/read

在 AAD B2C 门户中,您可以设置所有这些。

例如,在此示例中,客户端应用程序请求此 apiEndpoint 的权限https://fabrikamb2chello.azurewebsites.net/hello 通过this scope https://fabrikamb2c.onmicrosoft.com/helloapi/demo.read

在门户的 AAD B2C 部分中设置。在我的 Web Api(称为 HelloApi)中,我设置了范围: enter image description here

然后在同一个 AAD B2C 部分中,我转到我的客户端应用程序(称为 AzureAD B2C Playground),并在名为“API 访问”的部分中添加指向 HelloApi 的链接,并确定此应用程序将具有哪些范围进入。 enter image description here

回到客户端应用程序,我需要确保设置了 clientId、策略和重定向 Uri,并更新了 Web API 范围和 ApiEndpoint 的值。现在所有内容都已在 AAD B2C 门户中连接,您应该会取回该资源(Web Api)的访问 token 。

this sample for details

access tokens in AAD B2C

scopes in AAD B2C

关于c# - 为什么 AuthenticationResult B2C 认证中 accessToken 为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59728066/

相关文章:

docker - 在 Windows 和 WSL 之间共享 docker 凭据

c# - 强制 ThreadPool 更快地启动线程

c# - 当类相互依赖或外部数据时如何使用单元测试?

php - Symfony2 - 没有 session cookie 的有状态 API key 身份验证

facebook - 使用 oAuth 登录,我应该存储/使用什么来识别用户?

c# - 如何以编程方式登录到窗口 7,8 桌面

c# - 如何构造Dictionary <string,List <string >>的实例

c# - 将 Datatable 与另一个进行比较以查看其格式是否正确

php - PHP 中的错误请求

android - 如何频繁地从api获取数据?