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

时间:2018-04-20 22:35:29

标签: c# azure-ad-b2c

我正在使用https://github.com/Azure-Samples/active-directory-b2c-dotnet-webapp-and-webapi中的示例代码。当我收到授权代码时,回调方法\中的方法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 '<'.

1 个答案:

答案 0 :(得分:2)

问题在于,传递给/* * 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; } } 的{​​{1}}网址无效,并且返回了HTTP 404未找到错误。 Authority网址是使用ConfidentialClientApplicationAuthority值创建的。在我的情况下,TenantDefaultPolicy属性在Tenant属性之后被初始化,导致URL不包含正确的值。

DefaultPolicy

这导致Authority网址具有以下值: public static string Authority = String.Format(AadInstance, Tenant, DefaultPolicy); ... public static string Tenant = ConfigurationManager.AppSettings["ida:Tenant"]; public static string DefaultPolicy = SignUpSignInPolicyId;

vs

Authority

在我最初的搜索过程中,我发现了这个问题 https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/issues/720但没有解决方案。我已经在那里发布了我的答案,以帮助将来的任何人。