C# 如何在JSON中获取Exchange标识令牌?
Outlook API使用Office.context.mailbox.getUserIdentityTokenAsync(回调,userContext)返回“标识令牌”代码>()。我试图从base-64 URL编码字符串中提取JSON 到目前为止,我已经尝试了此url中的代码示例: Javascript:C# 如何在JSON中获取Exchange标识令牌?,c#,asp.net,office365,outlook-addin,exchangewebservices,C#,Asp.net,Office365,Outlook Addin,Exchangewebservices,Outlook API使用Office.context.mailbox.getUserIdentityTokenAsync(回调,userContext)返回“标识令牌”()。我试图从base-64 URL编码字符串中提取JSON 到目前为止,我已经尝试了此url中的代码示例: Javascript: Office.context.mailbox.getUserIdentityTokenAsync(function (data) { $.ajax({ type: "POS
Office.context.mailbox.getUserIdentityTokenAsync(function (data) {
$.ajax({
type: "POST",
url: "/api/exchange/createAndValidateIdentityToken",
contentType: 'application/json',
data: JSON.stringify({ userIdentityToken: data.value })
})
.done(function (data) {
console.log(data);
})
.fail(function (data) {
console.log(data);
});
});
[HttpPost]
public AppIdentityToken CreateAndValidateIdentityToken(JObject data)
{
JToken userIdentityToken = data.GetValue("userIdentityToken");
string rawToken = userIdentityToken.Value<string>();
try
{
AppIdentityToken token = (AppIdentityToken)AuthToken.Parse(rawToken);
token.Validate(new Uri("https://**url**/ews/exchange.asmx"));
return token;
}
catch (TokenValidationException ex)
{
throw new ApplicationException("A client identity token validation error occurred.", ex);
}
}
C#:
Office.context.mailbox.getUserIdentityTokenAsync(function (data) {
$.ajax({
type: "POST",
url: "/api/exchange/createAndValidateIdentityToken",
contentType: 'application/json',
data: JSON.stringify({ userIdentityToken: data.value })
})
.done(function (data) {
console.log(data);
})
.fail(function (data) {
console.log(data);
});
});
[HttpPost]
public AppIdentityToken CreateAndValidateIdentityToken(JObject data)
{
JToken userIdentityToken = data.GetValue("userIdentityToken");
string rawToken = userIdentityToken.Value<string>();
try
{
AppIdentityToken token = (AppIdentityToken)AuthToken.Parse(rawToken);
token.Validate(new Uri("https://**url**/ews/exchange.asmx"));
return token;
}
catch (TokenValidationException ex)
{
throw new ApplicationException("A client identity token validation error occurred.", ex);
}
}
[HttpPost]
公用AppIdentityToken CreateAndValidateIdentityToken(作业对象数据)
{
JToken userIdentityToken=data.GetValue(“userIdentityToken”);
字符串rawToken=userIdentityToken.Value();
尝试
{
AppIdentityToken令牌=(AppIdentityToken)AuthToken.Parse(rawToken);
验证(新Uri(“https://**url**/ews/exchange.asmx”);
返回令牌;
}
捕获(TokenValidationException ex)
{
抛出新的ApplicationException(“发生客户端身份令牌验证错误。”,例如);
}
}
AuthToken.Parse
返回一个充满异常的AppIdentityToken
,我不明白为什么:
注1:错误部分用法语表示:“类型的级别异常”=“引发了类型的异常”
注2:解码时的身份令牌格式:经过一整天的搜索,我终于找到了解决方案。以下是引导我找到解决方案的来源: 此链接表示,如果出现
InvalidTokenAudienceException
:
包含URL传递给Validate()时引发的异常
AppIdentityToken对象的方法与受众不匹配
在客户端标识令牌中指定的参数。
()
在JSON标识令牌中,有一个采用.html文件的aud
属性,并且:
令牌只有从中运行的外接程序发送时才有效
客户端的浏览器如果外接程序使用Office外接程序清单
schema v1.1,此URL是在第一个SourceLocation中指定的URL
元素,在表单类型ItemRead或ItemEdit下
()
以下代码适用于我:
[HttpPost]
public AppIdentityToken CreateAndValidateIdentityToken(JObject data)
{
JToken userIdentityToken = data.GetValue("userIdentityToken");
string rawToken = userIdentityToken.Value<string>();
try
{
AppIdentityToken token = (AppIdentityToken)AuthToken.Parse(rawToken);
token.Validate(new Uri("https://localhost:44300/AppRead/Home/Home.html"));
return token;
}
catch (TokenValidationException ex)
{
throw new ApplicationException("A client identity token validation error occurred.", ex);
}
}
[HttpPost]
公用AppIdentityToken CreateAndValidateIdentityToken(作业对象数据)
{
JToken userIdentityToken=data.GetValue(“userIdentityToken”);
字符串rawToken=userIdentityToken.Value();
尝试
{
AppIdentityToken令牌=(AppIdentityToken)AuthToken.Parse(rawToken);
验证(新Uri(“https://localhost:44300/AppRead/Home/Home.html"));
返回令牌;
}
捕获(TokenValidationException ex)
{
抛出新的ApplicationException(“发生客户端身份令牌验证错误。”,例如);
}
}
如果在调用Validate方法之前访问了令牌成员,则会调用这些异常。如果在调用Validate方法后检查令牌会发生什么情况?当我从令牌.Validate
行跳转时,它会转到catch
行:“发生了客户端标识令牌验证错误。”InnerException:“访问群体URI验证失败。访问群体不匹配。”我猜URI当时是错误的,他们在找什么?文件上不清楚,这是一个验证错误。看起来提供的URI和期望的URI不匹配。这可能只是猜测,但您可能希望在此处开始搜索…我不理解与URI的链接。这个XML来自哪里?