Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript atob(str) equivilent in c#

Tags:

javascript

c#

I did this:

 byte[] data = Convert.FromBase64String(str);
 string decodedString = Encoding.UTF8.GetString(data);
 Console.WriteLine(decodedString);

but got Unhandled Exception: System.FormatException: Invalid length for a Base-64 char array or string.

In javascript using atob(str) gives me the right decoded str.

javascript console:

atob("eyJpc3MiOiJodHRwczovL2lkZW50aXR5LXN0YWdpbmcuYXNjZW5kLnh5eiIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHktc3RhZ2luZy5hc2NlbmQueHl6L3Jlc291cmNlcyIsImNsaWVudF9pZCI6IjY5OTRBNEE4LTBFNjUtNEZFRC1BODJCLUM2ODRBMEREMTc1OCIsInNjb3BlIjpbIm9wZW5pZCIsInByb2ZpbGUiLCJzdWIucmVhZCIsImRhdGEud3JpdGUiLCJkYXRhLnJlYWQiLCJhbGcuZXhlY3V0ZSJdLCJzdWIiOiIzNzdjMDk1Yi03ODNiLTQ3ZTctOTdiMS01YWVkOThjMDM4ZmMiLCJhbXIiOiJleHRlcm5hbCIsImF1dGhfdGltZSI6MTQwNzYxNTUwNywiaWRwIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvMDg0MGM3NjAtNmY3Yi00NTU2LWIzMzctOGMwOTBlMmQ0NThkLyIsIm5hbWUiOiJwa3NAYXNjZW5kLnh5eiIsImV4cCI6MTQwNzgzNjcxMSwibmJmIjoxNDA3ODMzMTExfQ")
"{"iss":"https://identity-staging.ascend.xyz","aud":"https://identity-staging.ascend.xyz/resources","client_id":"6994A4A8-0E65-4FED-A82B-C684A0DD1758","scope":["openid","profile","sub.read","data.write","data.read","alg.execute"],"sub":"377c095b-783b-47e7-97b1-5aed98c038fc","amr":"external","auth_time":1407615507,"idp":"https://sts.windows.net/0840c760-6f7b-4556-b337-8c090e2d458d/","name":"[email protected]","exp":1407836711,"nbf":1407833111}"
like image 771
Poul K. Sørensen Avatar asked Aug 12 '14 09:08

Poul K. Sørensen


2 Answers

    var str = "eyJpc3MiOiJodHRwczovL2lkZW50aXR5LXN0YWdpbmcuYXNjZW5kLnh5eiIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHktc3RhZ2luZy5hc2NlbmQueHl6L3Jlc291cmNlcyIsImNsaWVudF9pZCI6IjY5OTRBNEE4LTBFNjUtNEZFRC1BODJCLUM2ODRBMEREMTc1OCIsInNjb3BlIjpbIm9wZW5pZCIsInByb2ZpbGUiLCJzdWIucmVhZCIsImRhdGEud3JpdGUiLCJkYXRhLnJlYWQiLCJhbGcuZXhlY3V0ZSJdLCJzdWIiOiIzNzdjMDk1Yi03ODNiLTQ3ZTctOTdiMS01YWVkOThjMDM4ZmMiLCJhbXIiOiJleHRlcm5hbCIsImF1dGhfdGltZSI6MTQwNzYxNTUwNywiaWRwIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvMDg0MGM3NjAtNmY3Yi00NTU2LWIzMzctOGMwOTBlMmQ0NThkLyIsIm5hbWUiOiJwa3NAYXNjZW5kLnh5eiIsImV4cCI6MTQwNzgzNjcxMSwibmJmIjoxNDA3ODMzMTExfQ";
    int mod4 = str.Length % 4;
    if (mod4 > 0)
    {
        str += new string('=', 4 - mod4);
    }

solved it in c#

like image 100
Poul K. Sørensen Avatar answered Oct 06 '22 00:10

Poul K. Sørensen


Use javascript's window.btoa function to encode the string in Base 64 Format in Javascript Frontend UI. To decode back same string in C# (the equivalent of javascript's window.atob function) please see the following code.

(Most probably you are trying to post back data (HTML in most cases as it requires btoa encoding to ensure the best compatibility)) back to the controller or you might creating custom basic basic authentication filter on server side)

string base64Encoded = "YmFzZTY0IGVuY29kZWQgc3RyaW5n";
string base64Decoded;
byte[] data = System.Convert.FromBase64String(base64Encoded);
base64Decoded = System.Text.ASCIIEncoding.ASCII.GetString(data);
Console.WriteLine(base64Decoded)

You can see its working sample at https://dotnetfiddle.net/abxwSw

like image 28
vibs2006 Avatar answered Oct 05 '22 22:10

vibs2006