c# - 如何获取 JSON 格式的 Exchange 身份 token ?

标签 c# asp.net office365 outlook-addin exchangewebservices

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,我不明白为什么:

Error IdentityToken

注意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/

相关文章:

c# - 在 gridview edititemtemplate 中绑定(bind)下拉列表

c# - 在 Windows10 UWP 应用程序上使用 SendInput 发送滚动命令

c# - 还有什么可以导致 WebResource.axd 出现 'Padding is invalid and cannot be removed"异常?

c# - 我可以在用户控件内创建内联函数/方法吗?

excel插件 |在 list 加载时在桌面 excel 上显示错误

c# - 如何从富文本框向word文档中插入文本

c# - 将子窗口移动并放置在父窗口附近 C#

c# - 如何在 C# 中应用多个 .Tag 属性?

api - 在 Office 365 中未正确创建全天事件

powershell - Powershell脚本列出用户所属的分发列表