简体   繁体   English

Azure AD B2c Xamarin 表单从不静默登录

[英]Azure AD B2c Xamarin Form never signs in silently

I followed the example from this repo https://github.com/Azure-Samples/active-directory-b2c-xamarin-native and got the authentication working.我按照这个 repo https://github.com/Azure-Samples/active-directory-b2c-xamarin-native中的示例进行了身份验证。 A user can register and authenticate perfectly.用户可以完美地注册和认证。

the problem is, after a user is authenticated when this code runs, it can never aquire a silent token.问题是,当此代码运行时用户通过身份验证后,它永远无法获取静默令牌。

   IEnumerable<IAccount> accounts = await _pca.GetAccountsAsync();
            AuthenticationResult authResult = await _pca.AcquireTokenSilent(B2CConstants.Scopes, GetAccountByPolicy(accounts, B2CConstants.AuthoritySignInSignUp))
               .WithB2CAuthority(B2CConstants.AuthoritySignInSignUp)
               .ExecuteAsync();

            var newContext = UpdateUserInfo(authResult);
            return newContext;

I have assumed this is about refresh tokens, but I have not found a suitable example on how to accomplish this.我假设这是关于刷新令牌,但我还没有找到一个合适的例子来说明如何做到这一点。

EDIT here is the policy config编辑这里是策略配置在此处输入图像描述

Unfortunately, I have found many times that MSAL is poorly documented.不幸的是,我多次发现 MSAL 的文档记录很差。 For this reason, many samples and apps that rely on it are not able to provide strong documentation either.因此,许多依赖它的示例和应用程序也无法提供强大的文档。

There is a github issue with a similar experience in another xamarin repository that uses the same identity client package:在使用相同身份客户端 package 的另一个 xamarin 存储库中存在具有类似经验的 github 问题:

https://github.com/Azure-Samples/active-directory-xamarin-native-v2/issues/38 https://github.com/Azure-Samples/active-directory-xamarin-native-v2/issues/38

Include="Microsoft.Identity.Client" version="4.13.0"

This issue links to the following articles:本期链接到以下文章:

https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-net-aad-b2c-considerations#resource-owner-password-credentials-ropc https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-net-aad-b2c-considerations#resource-owner-password-credentials-ropc

https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/msal-net-2-released#token-cache-index-keys https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/msal-net-2-released#token-cache-index-keys

Which indicates:这表明:

Known issue with Azure AD B2C MSAL.NET supports a token cache. Azure AD B2C MSAL.NET 的已知问题支持令牌缓存。 The token caching key is based on the claims returned by the identity provider (IdP).令牌缓存密钥基于身份提供者 (IdP) 返回的声明。 Currently, MSAL.NET needs two claims to build a token cache key:目前,MSAL.NET 需要两个声明来构建令牌缓存键:

  • tid (the Azure AD tenant ID) tid(Azure AD 租户 ID)
  • preferred_username首选用户名

Both of these claims may be missing in Azure AD B2C scenarios because not all social identity providers (Facebook, Google, and others) return them in the tokens they return to Azure AD B2C.A symptom of such a scenario is that MSAL.NET returns Missing from the token response when you access the preferred_username claim value in tokens issued by Azure AD B2C.在 Azure AD B2C 场景中可能缺少这两个声明,因为并非所有社交身份提供者(Facebook、Google 和其他)都在返回 Azure AD B2C 的令牌中返回它们。这种场景的症状是 MSAL.NET 返回当您访问 Azure AD B2C 颁发的令牌中的 preferred_username 声明值时,令牌响应中丢失。 MSAL uses the Missing from the token response value for preferred_username to maintain cache cross-compatibility between libraries. MSAL 使用 Preferred_username 的令牌响应值中的 Missing 来维护库之间的缓存交叉兼容性。

Some workarounds are provided in the Microsoft article, and the GitHub article suggests implementation changes: Microsoft 文章中提供了一些解决方法,GitHub 文章建议了实现更改:

Workarounds Mitigation for missing tenant ID The suggested workaround is to use caching by policy described earlier.解决方法 减轻缺少租户 ID 建议的解决方法是使用前面描述的策略缓存。

Alternatively, you can use the tid claim if you're using custom policies in Azure AD B2C.或者,如果您在 Azure AD B2C 中使用自定义策略,则可以使用 tid 声明。 Custom policies can return additional claims to your application by using claims transformation.自定义策略可以通过使用声明转换将其他声明返回到您的应用程序。

Mitigation for "Missing from the token response" One option is to use the name claim instead of preferred_username.缓解“缺少令牌响应”一种选择是使用名称声明而不是首选用户名。 To include the name claim in ID tokens issued by Azure AD B2C, select Display Name when you configure your user flow.在配置用户流时,在 Azure AD B2C、select 显示名称中包含名称声明。

For more information about specifying which claims are returned by your user flows, see Tutorial: Create user flows in Azure AD B2C.有关指定用户流返回哪些声明的更多信息,请参阅教程:在 Azure AD B2C 中创建用户流。

Hopefully, some of these discoveries can put you on the right path.希望其中一些发现可以使您走上正确的道路。

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

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