【问题标题】:Using Office Outlook API with hard-code user name and password使用带有硬编码用户名和密码的 Office Outlook API
【发布时间】:2016-12-03 16:08:35
【问题描述】:

我正在尝试构建一个 (C#) 网络应用程序,允许客户与我进行约会。 我希望 Web 应用程序能够读取条目并将其添加到我的 Outlook 日历中。 许多用户将使用网络应用程序,但网络应用程序只能访问一个 Outlook 日历 - 我的。 我能够开始工作的所有示例都涉及 Web 应用程序用户交互式身份验证 - 但我的用户不会知道我的密码。 我想在网络应用程序中硬编码我的用户名/电子邮件地址和密码。

尝试获取令牌时出现错误:

Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException:
AADSTS65001: The user or administrator has not consented to use the application.
Send an interactive authorization request for this user and resource.

我不是租户的管理员。有没有什么办法可以在没有管理员参与的情况下让它工作? 使用某种证书而不是用户名和密码作为用户凭据会有帮助吗?

我的代码(目前在一个简单的 C# 控制台应用程序中)如下:

UserCredential uc = new UserCredential(MyUsername, MyPassword);
var AuthContext = new AuthenticationContext("https://login.windows.net/Common");

// this doesn't work unless an unexpired token already exists
ar = AuthContext.AcquireToken("https://outlook.office365.com/", MyClientId, uc);

// this does work, but requires the app user to know the password
ar = AuthContext.AcquireToken("https://outlook.office365.com/", MyClientId, new Uri(MyReturnURI));

【问题讨论】:

    标签: authentication oauth outlook authorization office365


    【解决方案1】:

    要启用使用用户名和密码直接请求令牌,我们需要同意使用该应用程序。

    我们可以使用OAuth 2.0 授权码授权流程来授权用户。下面是使用 ADAL 身份验证库 (3.13.1.846) 获取委托令牌的示例:

     static string authority= "https://login.microsoftonline.com/common";
     public static string GetDeligateToken(string resource, string clientId,string redirectURL)
        {
            AuthenticationContext authContext = new AuthenticationContext(authority);
            AuthenticationResult authResult= authContext.AcquireTokenAsync(resource, clientId,new Uri(redirectURL), new PlatformParameters(PromptBehavior.Auto)).Result;
            return authResult.AccessToken;
        }
    

    在我们同意该应用后,现在我们可以使用您帖子中的代码来获取令牌。

    【讨论】:

    • 对不起,我不明白。如果我使用您提供的代码,那么它会将我发送到一个网页以登录(如果我提供我的用户名和密码,那么它会返回一个令牌)。但是该代码何时执行以及由谁执行?我尝试实现的网络应用程序将被不知道我的用户名和密码的其他用户使用。我是否打算执行您提前提供的代码,检索令牌并将其硬编码为我的 Web 应用程序中的字符串?我认为这些令牌会在一个小时左右后过期?
    • 运行帖子中的代码后,使用您要在原始帖子中使用的帐户登录页面。然后您可以看到一个同意页面,并且在您获得访问令牌后,您原始帖子中的问题应该得到修复(您已经同意使用该应用程序)。
    • 谢谢,这似乎有效。所以这个同意会永远持续下去?
    • 是的。除非您撤销同意记录。您可以管理同意here
    • 可以在不通过登录页面注销的情况下完成吗?完全以编程方式?
    猜你喜欢
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 2011-11-08
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 2011-02-05
    相关资源
    最近更新 更多