gpt4 book ai didi

c# - 如何在 JSON 中获取 Exchange Identity Token?

转载 作者:行者123 更新时间:2023-11-30 21:54:35 36 4
gpt4 key购买 nike

Outlook API 使用 Office.context.mailbox.getUserIdentityTokenAsync(callback, userContext); ( https://msdn.microsoft.com/en-us/library/office/fp142236.aspx ) 返回“身份 token ”。我正在尝试从 base-64 URL 编码字符串中获取 JSON。

到目前为止,我已经尝试了这个 url 中的代码示例: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,我不明白为什么:

Error IdentityToken

注意 1:错误部分是法语的:“a levé une exception de type”=“has raised an exception of 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 Identity Token?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32949382/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com