[英]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. 问题是,传递给
ConfidentialClientApplication
的Authority
URL无效,并且返回HTTP 404 not found错误。 The Authority
URL is created using the Tenant
and DefaultPolicy
values. Authority
URL是使用Tenant
和DefaultPolicy
值创建的。 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
属性之后初始化Tenant
和DefaultPolicy
属性,导致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.