c# - 在 outlook 插件中实现 outlook calendar api

标签 c# office365 outlook-addin office365-apps office365api

我正在尝试在 Outlook 365 加载项中实现 Office365 Outlook 日历 API。 Outlook 日历 API 在 Web 应用程序中完全实现。 OAuth2 和 Web 应用程序中返回的 auth_token 一切正常。

我在加载项内使用 OAuth2 登录时遇到问题。如果您在加载项中打开 Microsoft 的 OAuth2-Login,它会在您输入 appdev****@outlook[dot]com 帐户后打开一个 Internet Explorer 实例。这不适用于 session 中保存的 auth_token。

我尝试将 auth_token 保存在数据库中(请参阅//测试部分)并为加载项内的用户请求它。此错误带有 DataServiceClientException:Unauthorized 未知位置。

    [Route("SignIn")]
    public async Task<ActionResult> SignIn()
    {
        string authority = "https://login.microsoftonline.com/common";

        string clientId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        AuthenticationContext authContext = new AuthenticationContext(authority);

        Uri redirectUri = new Uri(Url.Action("Authorize", "outlook", null, HttpContext.Request.Scheme));

        Uri authUri = await authContext.GetAuthorizationRequestUrlAsync(scopes, null, clientId,
            redirectUri, UserIdentifier.AnyUser, null);

        return Redirect(authUri.ToString());
    }

    [Route("Authorize")]
    public async Task<ActionResult> Authorize()
    {
        string authCode = Request.Query["code"];

        string authority = "https://login.microsoftonline.com/common";

        string clientId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        string clientSecret = "xxxxxxxxxxxx";
        AuthenticationContext authContext = new AuthenticationContext(authority);

        Uri redirectUri = new Uri(Url.Action("Authorize", "outlook", null, HttpContext.Request.Scheme));

        ClientCredential credential = new ClientCredential(clientId, clientSecret);

        try
        {
            var authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(
                authCode, redirectUri, credential, scopes);

            HttpContext.Session.SetString("access_token", authResult.Token);
            HttpContext.Session.SetString("user_email", GetUserEmail(authContext, clientId));

            //*** TEST ***
            _dbContext.ApplicationUsers.FirstOrDefault(e => e.Email == "appdev****@outlook.com").AccessToken = authResult.Token;
            _dbContext.ApplicationUsers.FirstOrDefault(e => e.Email == "appdev****@outlook.com").Email = GetUserEmail(authContext, clientId);

            return Content("Access Token: " + authResult.Token + " Email: " + GetUserEmail(authContext, clientId));
        }
        catch (AdalException ex)
        {
            return Content(string.Format("ERROR retrieving token: {0}", ex.Message));
        }
    }

最佳答案

新答案 这是新一代 Office 加载项(以前称为 App for Office)和 OAUTH 身份验证的常见问题。加载项在沙盒 iFrame 中运行的事实强制在弹出窗口中进行身份验证。在父(沙盒 iFrame)窗口中检索身份验证 token 也存在一些问题,因为在此上下文中禁止框架通信。 我提出了一个解决方案here但最好的解决方案来自 Richard DiZerega,并提出了 here . 据我了解,您尝试将 auth_token 保存在数据库中,以便稍后 iFrame 加载项会请求它。它与 Richard DiZerega 的提议不符。

旧的错误答案 您遇到此问题是因为您可能将 Azure AD 应用程序注册为 Web 应用程序。现在您正在使用没有任何“url 位置”的 native 客户端请求它,这就是失败的原因。

native client 有不同的身份验证方案.

我认为这没什么大不了的 register another app在用于 native 客户端的 Azure AD 中(这是创建应用程序时提出的第一个问题)。

关于c# - 在 outlook 插件中实现 outlook calendar api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34733032/

相关文章:

c# - 从托管代码调用 Folder.PropertyAccessor 是否安全?

c# - "There was an error trying to log you in: ' ' "Blazor WebAssembly 使用 IdentiyServer 身份验证

c# - C# 中的 Web API 路由

javascript - JS Office Outlook 加载项 : get email as a file or just all email data

angular - 如果我无法确定生产中需要什么重定向 URI,我该如何使用 OAuth2?

sharepoint - 在 azure AD 中为 sharepoint online 创建一个应用程序

office365 - 在 Outlook 2016 中的 Office 365 组日历中创建 session 时禁用加载项

c# - 在 Unity 中捕获异常

c# - 删除 Entity Framework 中的对象映射多对多关系

Outlook 365 OAuth 535 5.7.3 认证不成功