System.Net.PrefixLookup.Lookup C# (CSharp) Method

Lookup() private method

private Lookup ( string lookupKey ) : object
lookupKey string
return object
        internal object Lookup(string lookupKey) {
            if (lookupKey==null) {
                return null;
            }
            object mostSpecificMatch = null;
            int longestMatchPrefix = 0;
            int prefixLen;
            lock (m_Store) {
                //
                // readers don't need to be locked, but we lock() because:
                // "The enumerator does not have exclusive access to the collection.
                //
                // When an enumerator is instantiated, it takes a snapshot of the current state
                // of the collection. If changes are made to the collection, such as adding,
                // modifying or deleting elements, the snapshot gets out of sync and the
                // enumerator throws an InvalidOperationException. Two enumerators instantiated
                // from the same collection at the same time can have different snapshots of the
                // collection."
                //
                // enumerate through every credential in the cache
                //
                string prefix;
                foreach (DictionaryEntry entry in m_Store) {
                    prefix = (string)entry.Key;
                    if (lookupKey.StartsWith(prefix)) {
                        prefixLen = prefix.Length;
                        //
                        // check if the match is better than the current-most-specific match
                        //
                        if (prefixLen>longestMatchPrefix) {
                            //
                            // Yes-- update the information about currently preferred match
                            //
                            longestMatchPrefix = prefixLen;
                            mostSpecificMatch = entry.Value;
                        }
                    }
                }
            }
            return mostSpecificMatch;
        }

Usage Example

        /// <devdoc>
        ///    <para>Pre-authenticates a request.</para>
        /// </devdoc>
        public static Authorization PreAuthenticate(WebRequest request, ICredentials credentials)
        {
            GlobalLog.Print("AuthenticationManager::PreAuthenticate() request:" + ValidationHelper.HashString(request) + " credentials:" + ValidationHelper.HashString(credentials));
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (credentials == null)
            {
                return(null);
            }

            HttpWebRequest        httpWebRequest = request as HttpWebRequest;
            IAuthenticationModule authenticationModule;

            if (httpWebRequest == null)
            {
                return(null);
            }

            //
            // PrefixLookup is thread-safe
            //
            string moduleName = s_ModuleBinding.Lookup(httpWebRequest.ChallengedUri.AbsoluteUri) as string;

            GlobalLog.Print("AuthenticationManager::PreAuthenticate() s_ModuleBinding.Lookup returns:" + ValidationHelper.ToString(moduleName));
            if (moduleName == null)
            {
                return(null);
            }
            authenticationModule = findModule(moduleName);
            if (authenticationModule == null)
            {
                // The module could have been unregistered
                // No preauthentication is possible
                return(null);
            }

            // Otherwise invoke the PreAuthenticate method
            // we're guaranteed that CanPreAuthenticate is true because we check before calling BindModule()
            Authorization authorization = authenticationModule.PreAuthenticate(request, credentials);

            if (authorization != null && !authorization.Complete && httpWebRequest != null)
            {
                httpWebRequest.CurrentAuthenticationState.Module = authenticationModule;
            }

            GlobalLog.Print("AuthenticationManager::PreAuthenticate() IAuthenticationModule.PreAuthenticate() returned authorization:" + ValidationHelper.HashString(authorization));
            return(authorization);
        }
All Usage Examples Of System.Net.PrefixLookup::Lookup
PrefixLookup