Outlook API 使用 Office.context.mailbox.getUserIdentityTokenAsync(callback, userContext);
返回“身份 token ”( https://msdn.microsoft.com/en-us/library/office/fp142236.aspx )。我正在尝试从 Base-64 URL 编码字符串中获取 JSON。
到目前为止,我已经尝试过此网址中的代码示例:https://msdn.microsoft.com/en-us/library/f7f4813a-3b2d-47bb-bf93-71b64620a56b
Javascript:
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);
});
});
C#:
[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);
}
}
AuthToken.Parse
返回一个充满异常的 AppIdentityToken
,我不明白为什么:
注意1:错误部分为法语:“a levé une exception de type”=“已引发类型异常”。
注2:解码后的身份 token 格式:https://msdn.microsoft.com/en-us/library/fp179838.aspx
最佳答案
经过一整天的寻找,终于找到了解决办法。以下是引导我找到解决方案的来源:
此链接表示如果出现 InvalidTokenAudienceException
:
Contains the exception thrown when the URL passed to the Validate() method of the AppIdentityToken object does not match the audience parameter specified in the client identity token. (https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.auth.validation(v=exchg.80).aspx)
在 JSON 身份 token 中,有一个采用 .html 文件的 aud
属性,并且:
A token is only valid if it is sent from the add-in that is running in the client's browser. If the add-in uses the Office Add-ins manifests schema v1.1, this URL is the URL specified in the first SourceLocation element, under the form type ItemRead or ItemEdit (https://msdn.microsoft.com/en-us/library/fp179838.aspx)
以下代码对我有用:
[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);
}
}
关于c# - 如何获取 JSON 格式的 Exchange 身份 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32949382/