示例#1
0
        public virtual ResourceAccessFailureResponse ReadAuthenticationHeader(string authenticateHeader, Uri resourceUri)
        {
            if (string.IsNullOrEmpty(authenticateHeader))
            {
                throw new ArgumentNullException("authenticateHeader");
            }
            if (resourceUri == null)
            {
                throw new ArgumentNullException("resourceUri");
            }

            ResourceAccessFailureResponse response = null;
            string expectedAuthType = "Bearer";
            string authType         = authenticateHeader.Split(new char[] { ' ' }, 2)[0];

            if (string.IsNullOrEmpty(authType))
            {
                throw new OAuthMessageSerializationException(string.Format(Resources.ID3741, authType));
            }

            NameValueCollection keyValuePairs = new NameValueCollection();

            if (authType.Contains(expectedAuthType))
            {
                response           = new ResourceAccessFailureResponse(resourceUri);
                authenticateHeader = authenticateHeader.Remove(0, authType.Length);
                authenticateHeader = authenticateHeader.TrimStart(new char[] { ' ' });
                if (!string.IsNullOrEmpty(authenticateHeader))
                {
                    string[] parameters = authenticateHeader.Split(new string[] { "\", " }, System.StringSplitOptions.None);
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        string   entry            = parameters[i];
                        string   splitAtEqualSign = "=\"";
                        string[] pairs            = entry.Split(new string[] { splitAtEqualSign }, 2, System.StringSplitOptions.None);
                        if (pairs.Length != 2)
                        {
                            throw new OAuthMessageSerializationException(string.Format(Resources.ID3741, authType));
                        }
                        if (i == parameters.Length - 1 && pairs[1][pairs[1].Length - 1] == '"')
                        {
                            pairs[1] = pairs[1].Remove(pairs[1].Length - 1, 1);
                        }
                        keyValuePairs.Add(pairs[0], pairs[1]);
                    }
                    response.Parameters.Add(keyValuePairs);
                    response.Validate();
                }
            }

            return(response);
        }
        public virtual ResourceAccessFailureResponse ReadAuthenticationHeader(string authenticateHeader, Uri resourceUri)
        {
            if (string.IsNullOrEmpty(authenticateHeader))
            {
                throw new ArgumentNullException("authenticateHeader");
            }
            if (resourceUri == null)
            {
                throw new ArgumentNullException("resourceUri");
            }

            ResourceAccessFailureResponse response = null;
            string expectedAuthType = "Bearer";
            string authType = authenticateHeader.Split(new char[] { ' ' }, 2)[0];

            if (string.IsNullOrEmpty(authType))
            {
                throw new OAuthMessageSerializationException(string.Format(Resources.ID3741, authType));
            }

            NameValueCollection keyValuePairs = new NameValueCollection();
            if (authType.Contains(expectedAuthType))
            {
                response = new ResourceAccessFailureResponse(resourceUri);
                authenticateHeader = authenticateHeader.Remove(0, authType.Length);
                authenticateHeader = authenticateHeader.TrimStart(new char[] { ' ' });
                if (!string.IsNullOrEmpty(authenticateHeader))
                {
                    string[] parameters = authenticateHeader.Split(new string[] { "\", " }, System.StringSplitOptions.None);
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        string entry = parameters[i];
                        string splitAtEqualSign = "=\"";
                        string[] pairs = entry.Split(new string[] { splitAtEqualSign }, 2, System.StringSplitOptions.None);
                        if (pairs.Length != 2)
                        {
                            throw new OAuthMessageSerializationException(string.Format(Resources.ID3741, authType));
                        }
                        if (i == parameters.Length - 1 && pairs[1][pairs[1].Length - 1] == '"')
                        {
                            pairs[1] = pairs[1].Remove(pairs[1].Length - 1, 1);
                        }
                        keyValuePairs.Add(pairs[0], pairs[1]);
                    }
                    response.Parameters.Add(keyValuePairs);
                    response.Validate();
                }
            }

            return response;
        }