Google's OpenIDConnect return a Base64 token that cannot be parsed

前端 未结 2 1207
礼貌的吻别
礼貌的吻别 2021-01-22 18:47

As exercise to understand OpenIDConnect, I am trying to authenticate in my web app with Google following this guide.

The problem is I cannot read the token that Google s

相关标签:
2条回答
  • 2021-01-22 19:13

    Use base64url decoding (instead of plain base64) after deserialization of the compact representation of the token as in:

    var http = new HttpClient(handler);
    var result = await http.PostAsync("https://www.googleapis.com/oauth2/v3/token", postData);
    var json = JObject.Parse(await result.Content.ReadAsStringAsync());
    var payload = json.id_token.split('.')[1];
    payload = payload.Replace('-', '+').Replace('_', '/');
    var base64 = payload.PadRight(payload.Length + (4 - payload.Length % 4) % 4, '=');
    var token = Convert.FromBase64String(base64);
    
    0 讨论(0)
  • 2021-01-22 19:31

    From this post:

    “id_token” is encoded in a format called JSON Web Token (JWT). JWT is the concatenation of “header”, “body”, “signature” by periods (.).

    So you need to split id_token on . and decode just the 2nd segment:

    var http = new HttpClient(handler);
    var result = await http.PostAsync("https://www.googleapis.com/oauth2/v3/token", postData);
    var json = JObject.Parse(await result.Content.ReadAsStringAsync());
    var token = Convert.FromBase64String(json.id_token.split('.')[1]);
    
    0 讨论(0)
提交回复
热议问题