exceptionsharepoint-onlinesharepoint-appssharepoint-clientobject

Sharepoint AudienceUriValidationFailedException while creating ClientContext with RER properties


I am using SharePoint Provider Hosted app. While RER triggers i am generating client context with help of Token helper function

using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(eventReceiverProperties))
{
            //some code
} 

Till today every thing is working fine but now i am getting following error

"XXXXXXXXX" is not the intended audience "c5925a97-ce7b-4291-a5de-e3f28e6e210f/spapp.mydomain.net@6a3dcb79-0795-408a-a4b0-6613d78b5eb2"

i didn't made any code and web config related changes. Above error i am getting from Token helper class

 public static SharePointContextToken ReadAndValidateContextToken(string contextTokenString, string appHostName = null)
    {
        JsonWebSecurityTokenHandler tokenHandler = CreateJsonWebSecurityTokenHandler();
        SecurityToken securityToken = tokenHandler.ReadToken(contextTokenString);
        JsonWebSecurityToken jsonToken = securityToken as JsonWebSecurityToken;
        SharePointContextToken token = SharePointContextToken.Create(jsonToken);

        string stsAuthority = (new Uri(token.SecurityTokenServiceUri)).Authority;
        int firstDot = stsAuthority.IndexOf('.');

        GlobalEndPointPrefix = stsAuthority.Substring(0, firstDot);
        AcsHostUrl = stsAuthority.Substring(firstDot + 1);

        tokenHandler.ValidateToken(jsonToken);

        string[] acceptableAudiences;
        if (!String.IsNullOrEmpty(HostedAppHostNameOverride))
        {
            acceptableAudiences = HostedAppHostNameOverride.Split(';');
        }
        else if (appHostName == null)
        {
            acceptableAudiences = new[] { HostedAppHostName };
        }
        else
        {
            acceptableAudiences = new[] { appHostName };
        }

        bool validationSuccessful = false;
        string realm = Realm ?? token.Realm;
        foreach (var audience in acceptableAudiences)
        {
            string principal = GetFormattedPrincipal(ClientId, audience, realm);
            if (StringComparer.OrdinalIgnoreCase.Equals(token.Audience, principal))
            {
                validationSuccessful = true;
                break;
            }
        }

        if (!validationSuccessful)
        {
            throw new AudienceUriValidationFailedException(
                String.Format(CultureInfo.CurrentCulture,
                "\"{0}\" is not the intended audience \"{1}\"", String.Join(";", acceptableAudiences), token.Audience));
        }

        return token;
    }

Is this error occurred because of latest o365 updates ? because since last 1 year my app is working fine.

Thanks Mohsin Pathan


Solution

  • We faced the same issue this week. The fix was to include this line in the 'appSettings' section of web.config:

        <add key="HostedAppHostNameOverride" value="spapp.mydomain.net" />
    

    More info on the subject