使用 Asp.Net Owin Oauth 进行增量 Google OAuth

2024-01-02

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

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

关于 Google Oauth 增量身份验证的文档...https://developers.google.com/accounts/docs/OAuth2WebServer#incrementalAuth 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

这是我想出的解决方案。它涉及在 url 中传递“scope”参数,然后在身份验证选项的“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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Asp.Net Owin Oauth 进行增量 Google OAuth 的相关文章

随机推荐

  • NA 替换为空格

    我使用了相同的方法将 NA 替换为空格或其他字符 但由于某种原因 这个方法不起作用 我想将数据框中的 NA 替换为空白 年份和年度列 我究竟做错了什么 shad 92 lt structure list year c 1992 NA NA
  • 通过图像中的边界框提取选定的文本

    我正在尝试通过图像上的边界框获取选定的文本 就像如果仅通过边界框选择单词一样 我想获取该文本并将其转换为文本文件 请查看我的代码并进行一些审查 以便我可以实现该功能 到目前为止 我已经将 PDF 文件转换为在文本上带有边框的图像 impor
  • 如何获取oracle中所有物化视图的列表

    如何获取所有物化视图的列表 尝试这个 SELECT FROM all snapshots 代替all snapshots您还可以使用all mviews view
  • 延迟加载角度的解析器

    有没有办法在加载延迟加载模块之前添加解析器 我尝试添加resolve到路由配置 但它没有被触发 也没有在网络上找到任何有用的东西 任何帮助 将不胜感激 import NgModule from angular core import Rou
  • 如何运行docker守护进程?

    我正在尝试使用命令运行 docker 守护进程 docker d 但我在控制台收到 unknown shorthand flag d in d 我应该怎么做才能运行 docker 守护进程 如果您停止了该服务 则需要以下命令才能重新启动它
  • 将下溢的无符号整数与 -1 进行比较是否定义明确?

    Consider the following size t r 0 r const bool result r 1 比较结果是否初始化result有明确的行为吗 它的结果是true 如我所料 This Q A was written bec
  • Excel 中的舍入函数、工作表函数与 VBA

    我有一个应用程序 用于返回与一大群值中的某些值最接近的匹配 如我之前的question https stackoverflow com questions 220966 lookup data in excel 我选择了 VBA 解决方案
  • Rails 3 视图中的 i18n Markdown 文件

    我目前正在迈克尔 哈特尔的工作导轨教程 http ruby railstutorial org ruby on rails tutorial book version 3 2同时尝试一些书中未涵盖的其他内容 完成后第5章 http ruby
  • 如何弱引用作为参数传递的函数

    我的视图控制器中有一个强引用模型 它有一个函数listenToQueue它 监听 我的 Firebase 数据库以获取实时更新 每当发生更新时 它都会将一个对象传递回我的视图控制器 然后视图控制器调用一个函数 这是我的代码 eventMod
  • 在图中绘制多个 geom-vline

    我正在尝试在图表中绘制两个 geom vline 下面的代码适用于一根垂直线 x 1 7 y 1 7 df1 data frame x x y y vertical lines lt c 2 5 ggplot df1 aes x x y y
  • 将日期列和时间列合并为日期时间列

    我有一个像这样的 Pandas 数据框 通过解析excel文件获得 COMPANY NAME MEETING DATE MEETING TIME YKSGR YAPI KRED S GORTA A 2013 12 16 00 00 00 1
  • 如何避免 Twitter Bootstrap 中的文本溢出?

    我是 Twitter Bootstrap 的新手 我写了以下 HTML div class span4 span class row fluid hideOverflow Html ActionLink item Name Details
  • java接口泛型转换问题

    List 类型中的方法 add capture 2 of extends IObject 不适用于参数 IDerived protected List
  • 改变函数中指针的值

    我想更改函数中变量的值 我的代码是这样的 void change char buf char str xxxxxxx buf str int main char xxx NULL change xxx 当我用 valgrind 调试时 它说
  • 在 Laravel 5.4 列中应用字符串函数

    我正在使用最新的 Laravel 5 4 我正在尝试进行一个简单的查询来按名称搜索用户 为 MySQL 编写的查询如下所示 SELECT FROM users WHERE upper name LIKE FOO 我正在努力让它与 Eloqu
  • Spring java.lang.NoSuchMethodError:org.springframework.expression.spel.SpelParserConfiguration

    在浏览器中打开控制器 url 时出现此错误 我读到这可能是依赖项问题 但每个 springframework jar 都有相同的 4 2 0 版本 除了现在未使用的 cql 来自服务器的完整跟踪 HTTP Status 500 Servle
  • Android-Volley:为 JsonArrayRequest 设置 HTTP 标头

    所以我看到了 JsonObjectRequests 的几个示例 其中添加了此代码 Override public String getBodyContentType return application json 有时会使用此代码 Over
  • IEnumerable重复函数

    我遇到了一个奇怪的问题 这里我重现了这个问题 Random r new Random List
  • sendgrid 给出错误 sendgrid.Email 不是构造函数

    我正在使用 sendgrid 发送电子邮件 但是当我尝试如下创建电子邮件对象时 let email new sendgrid Email email addTo email protected cdn cgi l email protect
  • 使用 Asp.Net Owin Oauth 进行增量 Google OAuth

    我正在寻找一种解决方案 使用 Asp Net 的 Owin OAuth 库对 Google 的 api 进行增量授权 我知道如何设置特定 api 的范围 但我想逐步进行 并且只能看到如何在全局范围内设置它 关于 Google Oauth 增