Using Google API GoogleJsonWebSignature.ValidateAsync(…) in server call

前端 未结 3 633
谎友^
谎友^ 2021-01-29 08:46

I am trying to implement Google SSO in my C# web application. It seemed to be pretty straightforward. Based on this tutorial, Google does its magic in the web browser

3条回答
  •  [愿得一人]
    2021-01-29 09:41

    My challenge is that it seems async methods can only be called by other async methods all the way back up the call-stack.

    It's not entirely true. You can use async method in sync methods. Of course you are losing most of the 'async' effect, but sometimes you have to. As async methods returs tasks you have some options.

    When Task is returning result, any reference to t.Result will block execution till it's known. Another option is to use Task.Wait(preferably with timeout). For example code to validate google jwt token:

    public bool ValidateGoogleToken(string token)
    {
      try
      {
        if(GoogleJsonWebSignature.ValidateAsync(token).Wait(1000))
          return true; //success
        //timeout exceeded
      }
      catch (Exception e)
      {
        //tampered token    
      }
      return false;
    }
    

提交回复
热议问题