使用RER属性创建ClientContext时Sharepoint AudienceUriValidationFailedException

时间:2018-03-06 14:20:22

标签: exception sharepoint-online sharepoint-apps sharepoint-clientobject

我正在使用SharePoint提供商托管应用。虽然RER触发器我在Token帮助函数的帮助下生成客户端上下文

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

直到今天,每件事情都运转良好,但现在我得到了以下错误

  

" XXXXXXXXX"不是目标受众   " c5925a97-ce7b-4291-a5de-e3f28e6e210f/spapp.mydomain.net@6a3dcb79-0795-408a-a4b0-6613d78b5eb2"

我没有做任何代码和网络配置相关的更改。我从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;
    }

是否因为最新的o365更新而发生此错误?因为自从过去1年以来我的应用程序运行良好。

由于 Mohsin Pathan

1 个答案:

答案 0 :(得分:1)

本周我们面临同样的问题。修复是将此行包含在web.config的“appSettings”部分中:

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

More info on the subject