2

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

1 Answers1

2

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

  • 1
    Thanks Niki , updating web.config work for me , But Since last 1 Year my app is working fine without this key "HostedAppHostNameOverride", how this problem occurs now ? Do you have any idea. – Mohasinkhan Pathan Mar 08 '18 at 10:43
  • 1
    Absolutely no idea, the app worked for us for nearly two years without this key and then out of the blue it just started throwing the above error. I would say environment update, but who knows... – Niki Spassov Mar 20 '18 at 12:21