0

I am working on a C# asp.net web application using Google Cloud Platform. I am using the Secret Manager feature where I am storing my Google ClientSecret and ClientId. Then, I am using the code below, which I know for sure that works since my lecturer used it, to obtain the client secret and id using the secret manager feature, using my Google cloud project name, my secretId and version as parameters.

public string GetGoogleClientId(string projectId, string SecretId, string version)
{
    SecretManagerServiceClient client = SecretManagerServiceClient.Create();

    SecretVersionName secretVersionName = new SecretVersionName(projectId, SecretId, version);

    AccessSecretVersionResponse result = client.AccessSecretVersion(secretVersionName);

    string payload = result.Payload.Data.ToStringUtf8().ToString();
    dynamic keys = JsonConvert.DeserializeObject(payload); //Gives the error here

    JObject jObject = JObject.Parse(payload);
    JToken jKey = jObject["Authentication:Google:ClientId"];
    return jKey.ToString();
}

However, I am getting the following error:

'Unexpected character encountered while parsing value: . Path '', line 0, position 0.'

The generated string in variable payload returns the following string that I am trying to serialize:

{
  "Authentication:Google:ClientSecret": "Hidden for security reasons",
  "Authentication:Google:ClientId": "Hidden for security reasons"
}

What am I doing wrong please?

  • Most likely `ToStringUtf8()` isn't properly stripping off a [BOM](https://en.wikipedia.org/wiki/Byte_order_mark). You should fix that method to remove the BOM e.g. by using a `StreamReader` as shown in [Encoding.UTF8.GetString doesn't take into account the Preamble/BOM](https://stackoverflow.com/q/11701341/3744182) and [How do I ignore the UTF-8 Byte Order Marker in String comparisons?](https://stackoverflow.com/a/2915239/3744182). – dbc Mar 25 '21 at 21:16
  • Ah yes, there is a BOM, when I copy/paste to https://dotnetfiddle.net/0NLkNN I can see it as an odd small character before the `{`. So this looks the be a duplicate of those questions. – dbc Mar 25 '21 at 21:19
  • Fixed version of `ToStringUtf8(this byte [] bytes)` here: https://dotnetfiddle.net/c4ivqk – dbc Mar 25 '21 at 21:23

0 Answers0