Google Oauth Approval Prompt for .net

2019-09-15 15:39发布

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

Here's my code:

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

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

1条回答
The star\"
2楼-- · 2019-09-15 16:16

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

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
查看更多
登录 后发表回答