Advertisement
Guest User

Untitled

a guest
Jan 13th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. public static bool TryParseBasicAuthentication(string header, out string username, out string password)
  2. {
  3. username = null; password = null;
  4.  
  5. try
  6. {
  7. if (!header.StartsWith("Basic "))
  8. return false;
  9.  
  10. // Remove the scheme and optional space after colon if present.
  11. header = header.Replace("Basic", "").TrimStart();
  12.  
  13. Encoding encoding = Encoding.GetEncoding("iso-8859-1");
  14. const char separator = ':';
  15. string unencodedAuth = encoding.GetString(Convert.FromBase64String(header));
  16.  
  17. if (!unencodedAuth.Contains(separator))
  18. return false;
  19.  
  20. int separatorIndex = unencodedAuth.IndexOf(separator);
  21. username = unencodedAuth.Substring(0, separatorIndex);
  22. password = unencodedAuth.Substring(separatorIndex + 1);
  23.  
  24. return true;
  25. }
  26. catch
  27. {
  28. return false;
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement