如何在 JSON 中获取 Exchange Identity Token?

How to get Exchange Identity Token in JSON?

Outlook API returns "an identity token" 使用 Office.context.mailbox.getUserIdentityTokenAsync(callback, userContext); (https://msdn.microsoft.com/en-us/library/office/fp142236.aspx)。我正在尝试从 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 returns AppIdentityToken 充满异常,我不明白为什么:

注意 1: 部分法语错误:"a levé une exception de type" = "has raised an exception of type".

注2:解码后的身份令牌格式:https://msdn.microsoft.com/en-us/library/fp179838.aspx

经过一整天的搜索,我终于找到了解决方案。以下是引导我找到解决方案的来源:

这 link 表示如果 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 身份令牌中,有一个 aud 属性采用 .html 文件,并且:

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);
        }

    }