c# - 如何在私有(private)浏览器 session 中询问用户许可?

标签 c# api google-calendar-api

我注意到 Google OAuth2 上的一个非常令人沮丧的情况,其中传递的电子邮件与实际连接到系统的帐户不同。让我更好地解释一下,我编写了这个方法来请求用户允许访问我的应用程序以访问用户私有(private) Google 日历:

public static CalendarService OAuth(string userName)
{
    string[] scopes = new string[]
    {
         CalendarService.Scope.Calendar,
         CalendarService.Scope.CalendarReadonly
    };

    try
    {
        UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
        {
             ClientId = "client id Google Developer Console",
             ClientSecret = "Secret key Google Developer Console"
        },
        scopes,
        userName,
        CancellationToken.None,
        new FileDataStore("Stored.Token")).Result;

        CalendarService service = new CalendarService(new BaseClientService.Initializer()
        {
              HttpClientInitializer = credential,
              ApplicationName = "Application name"
        });
      return service;
     }
     catch (Exception ex)
     {
          Console.WriteLine(ex.InnerException);
          return null;
     }
 } 

现在有人已经了解了情况,但我想解释一下为什么这个程序对我不利。假设我想创建一个应用程序,作为第一个屏幕允许用户插入个人电子邮件,该电子邮件应该被方法 OAuth 用作询问用户的参数 userName Google 浏览器窗口的权限。

到这里没问题,用户已经输入了电子邮件,应用程序打开了谷歌 Chrome 浏览器,向他询问访问私有(private)日历的权限。 但是,如果 Chrome 浏览器中实际连接的 Google 帐户与传递的电子邮件不同,会发生什么情况?

如果使用该应用程序的用户使用不同的连接帐户授予访问权限而他没有注意到这一点,会发生什么情况?

应用程序将使用不同的帐户上传数据,用户可以认为将上传数据保留在个人日历上。有人解决了这种情况,也许在私有(private)浏览器中的 UserCredential 代码块之后打开 Chrome,如果是,在这种情况下, token 将存储在指定的文件夹中:AppData\Roaming\Stored.Token?

实践示例:

1。用户在我的应用程序中输入私有(private)电子邮件:foo@gmail.com

2。应用程序启动 Chrome session 并请求用户许可,帐户已正确连接 bar@gmail.com

3。用户没有注意到这种情况并授予我的应用访问 bar@gmail.com

4。我的应用程序将使用 bar@gmail.com 进行上传事件,但用户认为该应用程序继续使用 foo@gmail.com

5。困惑。

最佳答案

可以通过执行 OAuth 流程并使用授予的 token 和范围更新私有(private)浏览器 session 来防止使用不同的帐户。

通读 Basics of Authentication ,我们有:

<强>1。注册您的应用

每个已注册的 OAuth 应用程序都被分配了一个唯一的客户端 ID 和客户端密码,绝不能共享。当您注册您的应用程序时,您可以填写除授权回调URL之外的所有信息,这也被认为是设置您的应用程序最重要的部分。用户认证成功后重定向到的回调地址。

<强>2。接受用户授权

您的客户端 ID 和客户端 key 来自应用程序的配置页面,建议将其存储为示例代码中所示的环境变量。

<html>
  <head>
  </head>
  <body>
    <p>
      Well, hello there!
    </p>
    <p>
      We're going to now talk to the GitHub API. Ready?
      <a href="https://github.com/login/oauth/authorize?scope=user:email&client_id=<%= client_id %>">Click here</a> to begin!</a>
    </p>
    <p>
      If that link doesn't work, remember to provide your own <a href="/v3/oauth/#web-application-flow">Client ID</a>!
    </p>
  </body>
</html>

请注意,URL 使用范围查询参数来定义应用程序请求的范围。对于示例代码,我们请求了用于读取电子邮件地址的 user:email 范围。单击链接后,您应该被带到授权页面。然后将被重定向到 Callback URL 中指定的路由。您将获得一个临时代码值,该代码值将添加到 POST HTTP 请求中以换取 access_token,然后您将能够以登录用户身份发出经过身份验证的请求。

# fetch user information
auth_result = JSON.parse(RestClient.get('https://api.github.com/user',
                                        {:params => {:access_token => access_token}}))

# if the user authorized it, fetch private emails
if has_user_email_scope
  auth_result['private_emails'] =
    JSON.parse(RestClient.get('https://api.github.com/user/emails',
                              {:params => {:access_token => access_token}}))
end

erb :basic, :locals => auth_result

最后,

<强>3。实现“持久”身份验证

按照建议:

Since we're persisting scopes within the session, we'll need to handle cases when the user updates the scopes after we checked them, or revokes the token. To do that, we'll use a rescue block and check that the first API call succeeded, which verifies that the token is still valid. After that, we'll check the X-OAuth-Scopes response header to verify that the user hasn't revoked the user:email scope.

实现开发人员 github 中显示的代码,我们现在有 authenticated 方法检查用户是否已经通过身份验证。如果不是,则调用 authenticate 方法,该方法执行 OAuth 流程并使用授予的 token 和范围更新 session 。

关于c# - 如何在私有(private)浏览器 session 中询问用户许可?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37006951/

相关文章:

c# - 从 ADO.NET 调用 Oracle 时批处理多个 select 语句

c# - 如何通过手动验证使 MobileNo、EmpOfficeCode 等实体在 EF core Razor 页面中唯一

python - 将 Google Picasa API 与 Python 结合使用

rest - Twilio 程序化语音不工作

ruby - 让 VCR 跳过 URI :

javascript - 如何根据描述对全日历事件进行颜色编码

c# - 使用动态键将 Json 解析为动态 c# 对象

c# - 如何在 StringBuilder 上添加 CheckBoxList 项用于 Linq

C# 相当于 VB6 中的 ccDebug

javascript - Google Calendar API v3 推送通知不尊重 timeMax 或 timeMin