.net的Google Oauth批准提示 [英] Google Oauth Approval Prompt for .net

查看:60
本文介绍了.net的Google Oauth批准提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在.NET中的Google Auth中设置Approval_Prompt = force?

How to set the Approval_Prompt=force in Google Auth in .NET?

这是我的代码:

Dim datafolder As String = Current.Server.MapPath("App_Data/CalendarService.api.auth.store")
    Dim scopes As IList(Of String) = New List(Of String)()
    Dim UserId As String = "GoogleID_" & pUsername        
    scopes.Add(CalendarService.Scope.Calendar)

    Dim myclientsecret As New ClientSecrets() With { _
      .ClientId = _ClientID, _
      .ClientSecret = _ClientSecret _
    }

    Dim flow As GoogleAuthorizationCodeFlow

    flow = New GoogleAuthorizationCodeFlow(New GoogleAuthorizationCodeFlow.Initializer() With { _
      .DataStore = New FileDataStore(datafolder), _
      .ClientSecrets = myclientsecret, _
      .Scopes = scopes _
    })


    Dim uri As String = Current.Request.Url.ToString() 'mengambil URL dari halaman ini

    Dim code = Current.Request("code")
    If code IsNot Nothing Then            
        Dim token = flow.ExchangeCodeForTokenAsync(UserId, code, uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result

        ' Extract the right state.
        Dim oauthState = AuthWebUtility.ExtracRedirectFromState(flow.DataStore, UserId, Current.Request("state")).Result            
        Current.Response.Redirect(oauthState)
    Else
        result = New AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId, CancellationToken.None).Result

        If result.RedirectUri IsNot Nothing Then
            ' Redirect the user to the authorization server.
            Current.Response.Redirect(result.RedirectUri)
        Else
            ' The data store contains the user credential, so the user has been already authenticated.
            'result.Credential.RevokeTokenAsync(CancellationToken.None)                
            myCalendarservice = New CalendarService(New BaseClientService.Initializer() With { _
              .ApplicationName = "My Calendar", _
              .HttpClientInitializer = result.Credential _
            })                
            'result.Credential.RevokeTokenAsync(CancellationToken.None)
        End If
    End If

我只想将Approval_Prompt设置为有效,但无法弄清楚...请有人帮助我...

All I want just to set the Approval_Prompt into Force, but cannot figure out... Please somebody help me...

推荐答案

我已经解决了我的问题.我在代码中手动添加了"approval_prompt = force"

I already solved my problem. I add manually "approval_prompt=force" in my code

暗流作为GoogleAuthorizationCodeFlow

Dim flow As GoogleAuthorizationCodeFlow

flow = New GoogleAuthorizationCodeFlow(New GoogleAuthorizationCodeFlow.Initializer() With { _
  .DataStore = New FileDataStore(datafolder), _
  .ClientSecrets = myclientsecret, _
  .Scopes = scopes _
})


Dim uri As String = Current.Request.Url.ToString() 'mengambil URL dari halaman ini

Dim code = Current.Request("code")
If code IsNot Nothing Then            
    Dim token = flow.ExchangeCodeForTokenAsync(UserId, code, uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result

    ' Extract the right state.
    Dim oauthState = AuthWebUtility.ExtracRedirectFromState(flow.DataStore, UserId, Current.Request("state")).Result            
    Current.Response.Redirect(oauthState)
Else
    result = New AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId, CancellationToken.None).Result

    If result.RedirectUri IsNot Nothing Then
        ' Redirect the user to the authorization server.
        Current.Response.Redirect(result.RedirectUri & "approval_prompt=force")
    Else
        ' The data store contains the user credential, so the user has been already authenticated.
        'result.Credential.RevokeTokenAsync(CancellationToken.None)                
        myCalendarservice = New CalendarService(New BaseClientService.Initializer() With { _
          .ApplicationName = "My Calendar", _
          .HttpClientInitializer = result.Credential _
        })                
        'result.Credential.RevokeTokenAsync(CancellationToken.None)
    End If
End If

这篇关于.net的Google Oauth批准提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆