简体   繁体   English

Microsoft.IdentityModel.Clients.ActiveDirectory.TokenResponse反序列化错误“遇到意外的字符'<'”

[英]Microsoft.IdentityModel.Clients.ActiveDirectory.TokenResponse deserialization error “Encountered unexpected character '<'”

I'm using the sample code from https://github.com/Azure-Samples/active-directory-b2c-dotnet-webapp-and-webapi . 我正在使用https://github.com/Azure-Samples/active-directory-b2c-dotnet-webapp-and-webapi中的示例代码。 When I receive an authorization code the method ConfidentialClientApplication.AcquireTokenByAuthorizationCodeAsync in the callback method OnAuthorizationCodeReceived throws the exception: 当我收到的授权码的方法ConfidentialClientApplication.AcquireTokenByAuthorizationCodeAsync在回调方法OnAuthorizationCodeReceived抛出异常:

System.Runtime.Serialization.SerializationException: There was an error deserializing the object of type Microsoft.Identity.Client.Internal.OAuth2.TokenResponse. Encountered unexpected character '<'.

Here's a snippet of the sample code: 这是示例代码的片段:

/*
 * Callback function when an authorization code is received 
 */
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification notification)
{
    // Extract the code from the response notification
    var code = notification.Code;

    string signedInUserID = notification.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
    TokenCache userTokenCache = new MSALSessionCache(signedInUserID, notification.OwinContext.Environment["System.Web.HttpContextBase"] as HttpContextBase).GetMsalCacheInstance();
    ConfidentialClientApplication cca = new ConfidentialClientApplication(ClientId, Authority, RedirectUri, new ClientCredential(ClientSecret), userTokenCache, null);
    try
    {
        AuthenticationResult result = await cca.AcquireTokenByAuthorizationCodeAsync(code, Scopes);
    }
    catch (Exception ex)
    {
        //TODO: Handle
        throw;
    }
}

The issue was that the Authority URL that was being passed to ConfidentialClientApplication was invalid and was returning a HTTP 404 not found error. 问题是,传递给ConfidentialClientApplicationAuthority URL无效,并且返回HTTP 404 not found错误。 The Authority URL is created using the Tenant and DefaultPolicy values. Authority URL是使用TenantDefaultPolicy值创建的。 In my case the Tenant and DefaultPolicy properties were being initialized after the Authority property resulting in the URL to not contain the proper values. 在我的情况下,在Authority属性之后初始化TenantDefaultPolicy属性,导致URL不包含正确的值。

public static string Authority = String.Format(AadInstance, Tenant, DefaultPolicy);
...
public static string Tenant = ConfigurationManager.AppSettings["ida:Tenant"];
public static string DefaultPolicy = SignUpSignInPolicyId;

This resulted in the Authority URL to have the value: https://login.microsoftonline.com/tfp///v2.0/.well-known/openid-configuration 这导致Authority URL具有以下值: https://login.microsoftonline.com/tfp///v2.0/.well-known/openid-configuration : https://login.microsoftonline.com/tfp///v2.0/.well-known/openid-configuration

vs

https://login.microsoftonline.com/tfp/my_tenant.onmicrosoft.com/my_policy/v2.0/.well-known/openid-configuration

During my initial search I found this issue reported to https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/issues/720 but no solution. 在我最初的搜索过程中,我发现此问题报告给https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/issues/720,但没有解决方案。 I've posted my answer there to help anyone in the future. 我已将我的答案贴在那里,将来可以帮助任何人。

暂无
暂无

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

相关问题 Microsoft.IdentityModel.Clients.ActiveDirectory.Platform不存在 - Microsoft.IdentityModel.Clients.ActiveDirectory.Platform not present 将 Microsoft.IdentityModel.Clients.ActiveDirectory 升级到 5.2.9 后 AcquireTokenByAuthorizationCodeAsync 抛出错误 - AcquireTokenByAuthorizationCodeAsync throws error after upgrading Microsoft.IdentityModel.Clients.ActiveDirectory to 5.2.9 Microsoft.IdentityModel.Clients.ActiveDirectory v3.17.0 和 appid/appsecret - Microsoft.IdentityModel.Clients.ActiveDirectory v3.17.0 and appid/appsecret 对Microsoft.IdentityModel.Clients.ActiveDirectory 3.14.2的硬依赖性 - Hard dependency on Microsoft.IdentityModel.Clients.ActiveDirectory 3.14.2 无法加载文件或程序集“ Microsoft.IdentityModel.Clients.ActiveDirectory.Platform” - Could not load file or assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory.Platform' 如何安装 Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms? - How do I install Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms? Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential是否不包含2个参数? - Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential does not take 2 parameters? 无法加载文件或程序集“Microsoft.IdentityModel.Clients.ActiveDirectory” - Could not load file or assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory' 替换为Microsoft.IdentityModel.Clients.ActiveDirectory.AuthorizationContext.GetAuthorizationRequestURL吗? - Replacement for Microsoft.IdentityModel.Clients.ActiveDirectory.AuthorizationContext.GetAuthorizationRequestURL? 缺少程序集引用Microsoft.IdentityModel.Clients.ActiveDirectory - Missing assembly reference Microsoft.IdentityModel.Clients.ActiveDirectory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM