コード例 #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));
            }

            Dictionary <string, string> keyValuePairs = new Dictionary <string, string>();

            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]);
                    }
                    foreach (var parameter in keyValuePairs)
                    {
                        response.Parameters.Add(parameter.Key, parameter.Value);
                    }
                    response.Validate();
                }
            }

            return(response);
        }
コード例 #2
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));
            }

            Dictionary<string,string> keyValuePairs = new Dictionary<string,string>();
            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]);
                    }
                    foreach (var parameter in keyValuePairs)
                    {
                        response.Parameters.Add(parameter.Key, parameter.Value);
                    }
                    response.Validate();
                }
            }

            return response;
        }