示例#1
0
        //
        // Authorization
        //

        private static IHeaderField ParseAuthorization(byte[] buffer, int valueOffset, int valueLength)
        {
            string value = Encoding.ASCII.GetString(buffer, valueOffset, valueLength);

            value = value.Trim();
            string[] valueData = value.Split(' ');

            string base64Credentials = valueData[1];

            byte[] credentialsBytes = Convert.FromBase64String(base64Credentials);
            string credentials      = Encoding.ASCII.GetString(credentialsBytes);

            string[] credData = credentials.Split(':');
            if (credData.Length != 2)
            {
                throw new HeaderFieldParserException("Invalid data of authorization value");
            }

            string userName = credData[0];
            string password = credData[1];

            AuthorizationHf authHf = new AuthorizationHf(userName, password);

            return(authHf);
        }
示例#2
0
        public bool Check(RequestHeader requestHeader, ResponseHeader response)
        {
            Regex r = new Regex(PathRegexMatch);

            if (r.IsMatch(requestHeader.Target.Path))
            {
                if (requestHeader.Contains(HFType.Authorization))
                {
                    AuthorizationHf authHf = requestHeader.GetSingleField <AuthorizationHf>(HFType.Authorization);
                    if (authHf.UserName == UserName && Password == authHf.Password)
                    {
                        return(true);
                    }
                }

                response.StatusCode = StatusCode.Unauthorized;
                response.Add(new WWWAuthenticateHf("authorization", "UTF-8"));
                return(false);
            }
            else
            {
                return(true);
            }
        }