Guest User

Untitled

a guest
Dec 11th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. // Although this function works it may not be the best way and has some bug.
  2. bool TryGetBasicAutheticationCredentials(string authorizationHeader, out string username, out string password)
  3. {
  4. const string schemePrefix = "Basic ";
  5.  
  6. if (authorizationHeader != null &&
  7. authorizationHeader.StartsWith(schemePrefix, System.StringComparison.OrdinalIgnoreCase))
  8. {
  9. try
  10. {
  11. string authorizationValue = authorizationHeader.Substring(schemePrefix.Length);
  12. byte[] credentialsBytes = System.Convert.FromBase64String(authorizationValue);
  13. string credentials = System.Text.Encoding.UTF8.GetString(credentialsBytes);
  14. string[] tokens = credentials.Split(':');
  15. username = tokens[0];
  16. password = tokens[1];
  17. return true;
  18. }
  19. catch
  20. {
  21. }
  22. }
  23.  
  24. username = default;
  25. password = default;
  26. return false;
  27. }
Add Comment
Please, Sign In to add comment