Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. public static class BasicAuthenticationUtil
  2. {
  3. /// <summary>
  4. /// Authorization ヘッダーの内容をパースします。
  5. /// </summary>
  6. /// <returns>
  7. /// パースに成功した場合は true を、失敗した場合は false を返します。
  8. /// </returns>
  9. public static bool ParseAuthorizationHeader(string auth,
  10. out string username, out string password)
  11. {
  12. username = password = string.Empty;
  13.  
  14. if (auth == null || auth.Substring(0, 5).ToLower() != "basic")
  15. { return false; }
  16.  
  17. string base64Credentials = auth.Substring(6);
  18.  
  19. byte[] bytesCredentials = Convert.FromBase64String(base64Credentials);
  20. string[] credentials = new UTF8Encoding().GetString(bytesCredentials).Split(':');
  21. username = credentials[0];
  22. password = credentials[1];
  23.  
  24. // Windows Vista sends username in the form of DOMAIN\User
  25. int delimiterIndex = username.IndexOf('\\');
  26. if (delimiterIndex != -1)
  27. { username = username.Remove(0, delimiterIndex + 1); }
  28.  
  29. return true;
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement