Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static bool TryParseBasicAuthentication(string header, out string username, out string password)
- {
- username = null; password = null;
- try
- {
- if (!header.StartsWith("Basic "))
- return false;
- // Remove the scheme and optional space after colon if present.
- header = header.Replace("Basic", "").TrimStart();
- Encoding encoding = Encoding.GetEncoding("iso-8859-1");
- const char separator = ':';
- string unencodedAuth = encoding.GetString(Convert.FromBase64String(header));
- if (!unencodedAuth.Contains(separator))
- return false;
- int separatorIndex = unencodedAuth.IndexOf(separator);
- username = unencodedAuth.Substring(0, separatorIndex);
- password = unencodedAuth.Substring(separatorIndex + 1);
- return true;
- }
- catch
- {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement