Advertisement
uwekeim

GetUserNameAndPassword

May 30th, 2017
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.48 KB | None | 0 0
  1. private bool GetUserNameAndPassword(HttpActionContext actionContext, out string username, out string password, out AuthType authType)
  2. {
  3.     // Originally from:
  4.     // https://simplesecurity.codeplex.com/SourceControl/latest#SimpleSecurity.Filters/BasicAuthorizeAttribute.cs
  5.     // for:
  6.     // https://stackoverflow.com/a/12646837/107625
  7.  
  8.     authType = AuthType.basic;
  9.     bool gotIt = false;
  10.     username = string.Empty;
  11.     password = string.Empty;
  12.     IEnumerable<string> headerVals;
  13.     if (actionContext.Request.Headers.TryGetValues("Authorization", out headerVals))
  14.     {
  15.         try
  16.         {
  17.             string authHeader = headerVals.FirstOrDefault();
  18.             char[] delims = {
  19.                 ' '
  20.             };
  21.             string[] authHeaderTokens = authHeader.Split(new char[] {
  22.                 ' '
  23.             });
  24.             if (authHeaderTokens[0].Contains("Basic"))
  25.             {
  26.                 string decodedStr = DecodeFrom64(authHeaderTokens[1]);
  27.                 string[] unpw = decodedStr.Split(new char[] {
  28.                     ':'
  29.                 });
  30.                 username = unpw[0];
  31.                 password = unpw[1];
  32.             }
  33.             else
  34.             {
  35.                 if (authHeaderTokens.Length > 1) username = DecodeFrom64(authHeaderTokens[1]);
  36.                 authType = AuthType.cookie;
  37.             }
  38.  
  39.             gotIt = true;
  40.         }
  41.         catch
  42.         {
  43.             gotIt = false;
  44.         }
  45.     }
  46.  
  47.     return gotIt;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement