【问题标题】:Incremental Google OAuth with Asp.Net Owin Oauth使用 Asp.Net Owin Oauth 的增量 Google OAuth
【发布时间】:2014-07-19 13:55:14
【问题描述】:

我正在寻找使用 Asp.Net 的 Owin OAuth 库对 Google 的 api 进行增量授权的解决方案。

我知道如何为特定 api 设置范围,但我想逐步进行,只能看到如何在全局范围内设置。

关于 Google Oauth 增量身份验证的文档... https://developers.google.com/accounts/docs/OAuth2WebServer#incrementalAuth

当前的 VB 代码...

Public Sub ConfigureAuth(app As IAppBuilder)

    Dim googleCreds = New GoogleOAuth2AuthenticationOptions() With {
                .ClientId = "xxxx",
                .ClientSecret = "xxx"
    }

    googleCreds.Scope.Add("https://www.googleapis.com/auth/analytics.readonly")
    app.UseGoogleAuthentication(googleCreds)

    ' Would like to add another way to specify GoogleDrive, YouTube, Google+ scopes
    ' Example code that doesn't work that would add a 2nd Google Oauth Listener
    googleCreds.Scope.Clear()
    googleCreds.Scope.Add("https://www.googleapis.com/auth/drive.file")
    googleCreds.AuthenticationType = "GoogleDrive"
    app.UseGoogleAuthentication(googleCreds)

End Class

【问题讨论】:

  • 好问题!我一直在动态设置 OWIN 范围、每个 Web 请求的 OWIN OAuth 范围、更改 OWIN OAuth 范围……应该支持开箱即用的 IMO,因为所有提供商似乎都鼓励它。

标签: asp.net oauth google-oauth owin


【解决方案1】:

这是我想出的解决方案。它涉及在 url 中传递一个“范围”参数,然后在身份验证选项的“OnApplyRedirect”函数中对其进行解析,然后手动将正确的范围 url 注入重定向 url。

    Dim googleCreds = New GoogleOAuth2AuthenticationOptions() With {
        .ClientId = "xxx",
        .ClientSecret = "xxx",
        .Provider = New Microsoft.Owin.Security.Google.GoogleOAuth2AuthenticationProvider() With { _
            .OnApplyRedirect = Function(context)
                                   Dim queryString = HttpContext.Current.Request.QueryString.ToString()
                                   Dim queryParms = HttpUtility.ParseQueryString(queryString)

                                   ' Change the value of "redirect" here
                                   ' e.g. append access_type=offline
                                   Dim redirect As String = context.RedirectUri
                                   redirect += "&access_type=offline"
                                   redirect += "&approval_prompt=force"
                                   redirect += "&include_granted_scopes=true"

                                   Dim uri = New Uri(redirect)

                                   If (Not String.IsNullOrEmpty(queryParms.Get("scope"))) Then
                                       Dim scope = queryParms.Get("scope")
                                       Dim redirectQueryString = HttpUtility.ParseQueryString(uri.Query)
                                       Select Case scope
                                           Case "Analytics"
                                               redirectQueryString.Set("scope", "https://www.googleapis.com/auth/analytics.readonly")
                                           Case "YoutTube"
                                               redirectQueryString.Set("scope", "https://gdata.youtube.com")
                                           Case "Drive"
                                               redirectQueryString.Set("scope", "https://www.googleapis.com/auth/drive.file")
                                           Case Else
                                               LoggingUtility.LogErrorMessage("Invalid scope passed in: scope: " + scope)
                                       End Select
                                       redirect = uri.GetLeftPart(UriPartial.Path) + "?" + redirectQueryString.ToString()
                                   End If

                                   context.Response.Redirect(redirect)

                               End Function, _
        }
    }

    'Google Analytics
    app.UseGoogleAuthentication(googleCreds)

【讨论】:

    【解决方案2】:

    在 OWIN 3.1 版中,增量身份验证按预期工作。 (它可能在早期版本中也能正常工作,我还没有测试过)。

    有两个技巧需要让这个工作:

    OWIN 预计 401

    如果你尝试打电话:

    var ctx = HttpContext.Current.Request.GetOwinContext();
    var properties = new AuthenticationProperties
    {
        RedirectUri = "/your/redirect",
    };
    
    ctx.Authentication.Challenge(properties, "Google");
    

    登录到您的应用程序时,不会发生从中间件对 Google 的调用。

    这可能会令人困惑,因为在登录期间使用完全相同的代码时它可以正常工作。

    原因是中间件只有在当前 HTTP 响应为 401 时才起作用,所以在上面的调用之后你需要这样的东西:

    Page.Response.StatusCode = 401;
    Page.Response.End();
    

    范围规范很挑剔

    在我尝试使用新范围调用的原始代码中,我有这样的事情:

    var ctx = HttpContext.Current.Request.GetOwinContext();
    var properties = new AuthenticationProperties
    {
        RedirectUri = redirect,
    };   
    
    properties.Dictionary["scope"] ="https://www.googleapis.com/auth/calendar.readonly";
    

    这按预期调用了 Google,并且 Goggle 根据请求的范围正确地进行了挑战,但随后返回到我的服务器的响应中断(从未找到位置)。

    我发现我需要做的就是重新发送我所有的基本作用域:

    var scopes = new List<string>
    {
        "https://www.googleapis.com/auth/plus.me",
        "https://www.googleapis.com/auth/userinfo.email",
        "https://www.googleapis.com/auth/userinfo.profile",
    };
    
    if (additionalScopes != null)
    {
        scopes = scopes.Union(additionalScopes).ToList();
    }
    
    properties.Dictionary["scope"] = string.Join(" ", scopes);
    

    【讨论】:

      猜你喜欢
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      • 1970-01-01
      相关资源
      最近更新 更多