.net core MSAL TokenAcquisition GetAccessTokenOnBehalfourUser始终失败,因为getaccounts始终为空

.net core MSAL TokenAcquisition GetAccessTokenOnBehalfourUser始终失败,因为getaccounts始终为空,.net-core,azure-active-directory,openid-connect,bearer-token,msal,.net Core,Azure Active Directory,Openid Connect,Bearer Token,Msal,我一直在尝试使用AzureAD MSAL和ADAL,但从未能够检索到令牌。我已经尝试了所有的示例,并且一直在讨论同一个问题,创建了令牌,并将其添加到EF Cahce DB中,但是当tokenAquisition对象尝试检索它时,找不到任何帐户并且无法获取令牌 我已经阅读了GitHub上的大部分(如果不是全部的话)问题,等等。这似乎对其他人有效,但似乎许多人都有相同的问题,我还没有看到其他答案,然后指向我尝试过的样本 目前的简单问题-如何从IConfidentialClientApplicatio

我一直在尝试使用AzureAD MSAL和ADAL,但从未能够检索到令牌。我已经尝试了所有的示例,并且一直在讨论同一个问题,创建了令牌,并将其添加到EF Cahce DB中,但是当tokenAquisition对象尝试检索它时,找不到任何帐户并且无法获取令牌

我已经阅读了GitHub上的大部分(如果不是全部的话)问题,等等。这似乎对其他人有效,但似乎许多人都有相同的问题,我还没有看到其他答案,然后指向我尝试过的样本

目前的简单问题-如何从
IConfidentialClientApplication
获取帐户

我从来没有得到过一个帐户或帐户列表

创建应用程序对象:

var app = ConfidentialClientApplicationBuilder.CreateWithApplicationOptions(_applicationOptions)
               .WithRedirectUri(currentUri)
               .WithAuthority(authority)
               .Build();
getAccessTokenOnBehalfourUser
中:

--> IAccount account = await application.GetAccountAsync(accountIdentifier);
返回空值

返回空列表/ienumarables

我希望从
application.GetAccountAsync(accountIdentifier)

以及
wait application.GetAccountsAsync()中的列表

好的,终于找到了我的问题

问题在于使用ASPNet identity登录AzureAD作为外部机构,但使用该身份登录并创建索赔原则

我从我的声明中找到了AzureAD ObjectIdentifier。因此,解决方案似乎是将ObjectIdentifier添加到标识中。我通过使用ClaimStransoformation并查找auth类型来实现这一点。如果它不是Identity.Application,则它来自AzureAD,检查用户是否拥有UserClaim,如果没有,则添加它。然后,该索赔被拾起并放入委托人的索赔中,然后在封面下,现在账户被找到了

        if (principal.HasClaim(c => c.Type == SecurityConstants.ClaimTypes.ObjectId))
        {
            string oId = principal.FindFirstValue(SecurityConstants.ScpcClaimTypes.ObjectId);

            var user = _usrMgr.FindByNameAsync(usrNm).Result;

            List<Claim> claims = new List<Claim>(_usrMgr.GetClaimsAsync(user).Result);
            if (!claims.Exists(c => c.Type == SecurityConstants.ScpcClaimTypes.ObjectId))
            {
                _usrMgr.AddClaimAsync(user, new Claim(SecurityConstants.ScpcClaimTypes.ObjectId, oId));
            }
if(principal.HasClaim(c=>c.Type==SecurityConstants.ClaimTypes.ObjectId))
{
字符串oId=principal.FindFirstValue(SecurityConstants.ScpcClaimTypes.ObjectId);
var user=\u usrMgr.FindByNameAsync(usrNm).Result;
列表声明=新列表(_usrMgr.GetClaimsAsync(user.Result));
如果(!claims.Exists(c=>c.Type==SecurityConstants.ScpcClaimTypes.ObjectId))
{
_usrMgr.addClaimesync(用户,新索赔(SecurityConstants.ScpcClaimTypes.ObjectId,oId));
}

您能否发送您主要依赖的示例的链接?Azure AD示例-active-directory-aspnetcore-webapp-openidconnect-v2-2.2令牌缓存-设置基于EF的令牌缓存我尝试了许多不同的实现来尝试使令牌缓存工作,以便我可以获取和使用ID令牌。我对您的错误表示抱歉初始化。我运行了这个示例,它对我有效。创建了DB,我在其中有一行带有我的令牌。我注意到这个示例实际上是一个教程,因此服务的配置在上一章中。另外,还有一些行需要取消注释,以便为您创建DB。请确保您阅读了说明令人遗憾的是。我不明白您是如何获得令牌表单缓存的-我做了一些修改,但尚未获得令牌缓存以检索令牌-内存、会话或EF6。我看到使用AuthCode创建的令牌很好-但当我尝试检索令牌时-如果我可以,我无法从应用程序中获取帐户获取一个用于获取帐户的示例,而无需构建自己的令牌提供者。
        if (principal.HasClaim(c => c.Type == SecurityConstants.ClaimTypes.ObjectId))
        {
            string oId = principal.FindFirstValue(SecurityConstants.ScpcClaimTypes.ObjectId);

            var user = _usrMgr.FindByNameAsync(usrNm).Result;

            List<Claim> claims = new List<Claim>(_usrMgr.GetClaimsAsync(user).Result);
            if (!claims.Exists(c => c.Type == SecurityConstants.ScpcClaimTypes.ObjectId))
            {
                _usrMgr.AddClaimAsync(user, new Claim(SecurityConstants.ScpcClaimTypes.ObjectId, oId));
            }