Basic Authentication (for Google Checkout)


/ Published in: C#
Save to your folder(s)

Using GCheckout.dll

Join("") is an IEnumerable extension I wrote (to concatenate strings with a separator)
Base64Decode() is a string extension I wrote (decode base64)

Configuration.Google[Test]Merchant{Id|Key} are just some static variables holding my info.


Copy this code and paste it in your HTML
  1. private GCheckout.EnvironmentType CheckAuthorization(string authHeader)
  2. {
  3. if (string.IsNullOrEmpty(authHeader))
  4. {
  5. Logger.Debug("No Authorization Header");
  6. return GCheckout.EnvironmentType.Unknown;
  7. }
  8.  
  9. if (authHeader.Contains("Basic"))
  10. authHeader = authHeader.Substring(authHeader.IndexOf("Basic") + 5);
  11.  
  12. var base64Auth = authHeader.Trim().Base64Decode();
  13.  
  14. var userPass = base64Auth.Split(':');
  15.  
  16. if (userPass.Length < 2)
  17. {
  18. Logger.Debug("Authorization Header was malformed. Raw: " + authHeader + ", Base64Decoded: " + base64Auth);
  19. return GCheckout.EnvironmentType.Unknown;
  20. }
  21. if (userPass.Length > 2)
  22. userPass[1] = userPass.Skip(1).Join("");
  23.  
  24. if (userPass[0] == Configuration.GoogleMerchantId &&
  25. userPass[1] == Configuration.GoogleMerchantKey)
  26. {
  27. return GCheckout.EnvironmentType.Production;
  28. }
  29. else if (userPass[0] == Configuration.GoogleTestMerchantId &&
  30. userPass[1] == Configuration.GoogleTestMerchantKey)
  31. {
  32. return GCheckout.EnvironmentType.Sandbox;
  33. }
  34. else
  35. {
  36. return GCheckout.EnvironmentType.Unknown;
  37. }
  38. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.