uwekeim icon

GetUserNameAndPassword

uwekeim | PRO | 05/30/17 08:18:19 PM UTC | 0 ⭐ | 1358 👁️ | Never ⏰ | []
C# |

1.48 KB

|

None

|

0 👍

/

0 👎

private bool GetUserNameAndPassword(HttpActionContext actionContext, out string username, out string password, out AuthType authType)
{
    // Originally from:
    // https://simplesecurity.codeplex.com/SourceControl/latest#SimpleSecurity.Filters/BasicAuthorizeAttribute.cs
    // for:
    // https://stackoverflow.com/a/12646837/107625
 
    authType = AuthType.basic;
    bool gotIt = false;
    username = string.Empty;
    password = string.Empty;
    IEnumerable<string> headerVals;
    if (actionContext.Request.Headers.TryGetValues("Authorization", out headerVals))
    {
        try
        {
            string authHeader = headerVals.FirstOrDefault();
            char[] delims = {
                ' '
            };
            string[] authHeaderTokens = authHeader.Split(new char[] {
                ' '
            });
            if (authHeaderTokens[0].Contains("Basic"))
            {
                string decodedStr = DecodeFrom64(authHeaderTokens[1]);
                string[] unpw = decodedStr.Split(new char[] {
                    ':'
                });
                username = unpw[0];
                password = unpw[1];
            }
            else
            {
                if (authHeaderTokens.Length > 1) username = DecodeFrom64(authHeaderTokens[1]);
                authType = AuthType.cookie;
            }
 
            gotIt = true;
        }
        catch
        {
            gotIt = false;
        }
    }
 
    return gotIt;
}

Comments