使用 RER 属性创建 ClientContext 时出现 Sharepoint AudienceUriValidationFailedException
Sharepoint AudienceUriValidationFailedException while creating ClientContext with RER properties
我正在使用 SharePoint 提供商托管的应用程序。当 RER 触发时,我在令牌辅助函数
的帮助下生成客户端上下文
using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(eventReceiverProperties))
{
//some code
}
直到今天一切正常,但现在我收到以下错误
"XXXXXXXXX" is not the intended audience
"c5925a97-ce7b-4291-a5de-e3f28e6e210f/spapp.mydomain.net@6a3dcb79-0795-408a-a4b0-6613d78b5eb2"
我没有进行任何与代码和网络配置相关的更改。以上错误是我从令牌助手 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 年以来,我的应用程序运行良好。
谢谢
莫辛帕坦
我们本周遇到了同样的问题。解决方法是将此行包含在 web.config 的 'appSettings' 部分:
<add key="HostedAppHostNameOverride" value="spapp.mydomain.net" />
我正在使用 SharePoint 提供商托管的应用程序。当 RER 触发时,我在令牌辅助函数
的帮助下生成客户端上下文using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(eventReceiverProperties))
{
//some code
}
直到今天一切正常,但现在我收到以下错误
"XXXXXXXXX" is not the intended audience "c5925a97-ce7b-4291-a5de-e3f28e6e210f/spapp.mydomain.net@6a3dcb79-0795-408a-a4b0-6613d78b5eb2"
我没有进行任何与代码和网络配置相关的更改。以上错误是我从令牌助手 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 年以来,我的应用程序运行良好。
谢谢 莫辛帕坦
我们本周遇到了同样的问题。解决方法是将此行包含在 web.config 的 'appSettings' 部分:
<add key="HostedAppHostNameOverride" value="spapp.mydomain.net" />