0

I am building a Web API for which the plan is to do OAuth Authentication using Azure AD. The API code is using Microsoft's Owin library and Identity.Web to validate the token once received in HTTP Request header. The challenge is that when I generate client secret from Azure AD and provide that in Web API code to validate received token, there is an error - The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

Have tried generate client secret multiple times but all trial has resulted in invalid base64 secret. Is there a way to generate base64 compatible client secret from Azure AD because MS Own library needs it that way? Can Azure AD be used for OAuth authentication?

Thanking all in advance.

MSLearner
  • 3
  • 2

1 Answers1

0

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

The above error occurs that when your token is not a valid Base64 encoded string and also you should verify that the base64string length is a multiple of 4 and that it is valid.

In order to correct this, you may have to try to pad it and change - to + and to / as follows:


public static byte[] DecodeUrlBase64(string s)

{

s = s.Replace('-', '+').Replace('_', '/').PadRight(4*((s.Length+3)/4), '=');

return Convert.FromBase64String(s);

}

Your code required encodedbase64 format but token is decoded base64 this may cause the error so try the above code.

Reference:

c# - The input is not a valid Base-64 string as it contains a non-base 64 character - Stack Overflow

Venkatesan
  • 3,748
  • 1
  • 3
  • 15
  • Thanks for replying. generating client secret from Azure portal - under app registration, there is certificates and secrets. This secret has ~ and _ in it, making it invalid base64. Replace can’t cover all invalid characters, there can be more invalids base 64 chars other than underscore, hyphen, tilde etch – MSLearner Aug 30 '22 at 05:08