Пример #1
2
    // properties

    // methods

        /// <devdoc>
        /// <para>Adds a <see cref='System.Net.NetworkCredential'/>
        /// instance to the credential cache.</para>
        /// </devdoc>
        // UEUE
        public void Add(Uri uriPrefix, string authType, NetworkCredential cred) {
            //
            // parameter validation
            //
            if (uriPrefix==null) {
                throw new ArgumentNullException("uriPrefix");
            }
            if (authType==null) {
                throw new ArgumentNullException("authType");
            }
            if ((cred is SystemNetworkCredential)
#if !FEATURE_PAL
                && !((string.Compare(authType, NtlmClient.AuthType, StringComparison.OrdinalIgnoreCase)==0)
                     || (DigestClient.WDigestAvailable && (string.Compare(authType, DigestClient.AuthType, StringComparison.OrdinalIgnoreCase)==0))
                     || (string.Compare(authType, KerberosClient.AuthType, StringComparison.OrdinalIgnoreCase)==0)
                     || (string.Compare(authType, NegotiateClient.AuthType, StringComparison.OrdinalIgnoreCase)==0))
#endif
                ) {
                throw new ArgumentException(SR.GetString(SR.net_nodefaultcreds, authType), "authType");
            }

            ++m_version;

            CredentialKey key = new CredentialKey(uriPrefix, authType);

            GlobalLog.Print("CredentialCache::Add() Adding key:[" + key.ToString() + "], cred:[" + cred.Domain + "],[" + cred.UserName + "]");

            cache.Add(key, cred);
            if (cred is SystemNetworkCredential) {
                ++m_NumbDefaultCredInCache;
            }
        }
Пример #2
0
    // properties

    // methods

        /// <include file='doc\CredentialCache.uex' path='docs/doc[@for="CredentialCache.Add"]/*' />
        /// <devdoc>
        /// <para>Adds a <see cref='System.Net.NetworkCredential'/>
        /// instance to the credential cache.</para>
        /// </devdoc>
        // UEUE
        public void Add(Uri uriPrefix, string authType, NetworkCredential cred) {
            //
            // parameter validation
            //
            if (uriPrefix==null) {
                throw new ArgumentNullException("uriPrefix");
            }
            if (authType==null) {
                throw new ArgumentNullException("authType");
            }
            if ((cred is SystemNetworkCredential)
                && !((String.Compare(authType, "NTLM", true, CultureInfo.InvariantCulture) == 0)
                     || (String.Compare(authType, "Kerberos", true, CultureInfo.InvariantCulture) == 0)
                     || (String.Compare(authType, "Negotiate", true, CultureInfo.InvariantCulture) == 0))) {
                throw new ArgumentException(SR.GetString(SR.net_nodefaultcreds, authType), "authType");
            }

            ++m_version;

            CredentialKey key = new CredentialKey(uriPrefix, authType);

            GlobalLog.Print("CredentialCache::Add() Adding key:[" + key.ToString() + "], cred:[" + cred.Domain + "],[" + cred.UserName + "],[" + cred.Password + "]");

            cache.Add(key, cred);
            if (cred is SystemNetworkCredential) {
                ++m_NumbDefaultCredInCache;
            }
        }
    // properties

    // methods

        /// <devdoc>
        /// <para>Adds a <see cref='System.Net.NetworkCredential'/>
        /// instance to the credential cache.</para>
        /// </devdoc>
        // UEUE
        public void Add(Uri uriPrefix, string authType, NetworkCredential cred) {
            //
            // parameter validation
            //
            if (uriPrefix==null) {
                throw new ArgumentNullException("uriPrefix");
            }
            if (authType==null) {
                throw new ArgumentNullException("authType");
            }
            if ((cred is SystemNetworkCredential)
                ) {
                throw new ArgumentException(SR.GetString(SR.net_nodefaultcreds, authType), "authType");
            }

            ++m_version;

            CredentialKey key = new CredentialKey(uriPrefix, authType);

            GlobalLog.Print("CredentialCache::Add() Adding key:[" + key.ToString() + "], cred:[" + cred.Domain + "],[" + cred.UserName + "]");

            cache.Add(key, cred);
            if (cred is SystemNetworkCredential) {
                ++m_NumbDefaultCredInCache;
            }
        }
Пример #4
0
        public override bool Equals(object comparand)
        {
            CredentialKey comparedCredentialKey = comparand as CredentialKey;

            if (comparand == null)
            {
                // This covers also the compared==null case
                return(false);
            }

            bool equals =
                string.Equals(AuthenticationType, comparedCredentialKey.AuthenticationType, StringComparison.OrdinalIgnoreCase) &&
                UriPrefix.Equals(comparedCredentialKey.UriPrefix);

            GlobalLog.Print("CredentialKey::Equals(" + ToString() + ", " + comparedCredentialKey.ToString() + ") returns " + equals.ToString());

            return(equals);
        }
Пример #5
0
        public NetworkCredential?GetCredential(Uri uriPrefix, string authType)
        {
            ArgumentNullException.ThrowIfNull(uriPrefix);
            ArgumentNullException.ThrowIfNull(authType);

            if (_cache == null)
            {
                if (NetEventSource.Log.IsEnabled())
                {
                    NetEventSource.Info(this, "CredentialCache::GetCredential short-circuiting because the dictionary is null.");
                }
                return(null);
            }

            int longestMatchPrefix = -1;
            NetworkCredential?mostSpecificMatch = null;

            // Enumerate through every credential in the cache
            foreach (KeyValuePair <CredentialKey, NetworkCredential> pair in _cache)
            {
                CredentialKey key = pair.Key;

                // Determine if this credential is applicable to the current Uri/AuthType
                if (key.Match(uriPrefix, authType))
                {
                    int prefixLen = key.UriPrefixLength;

                    // 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  = pair.Value;
                    }
                }
            }

            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Info(this, $"Returning {(mostSpecificMatch == null ? "null" : "(" + mostSpecificMatch.UserName + ":" + mostSpecificMatch.Domain + ")")}");
            }

            return(mostSpecificMatch);
        }
Пример #6
0
        /// <devdoc>
        ///  <para>
        ///    Removes a <see cref='System.Net.NetworkCredential'/> instance from the credential cache.
        ///  </para>
        /// </devdoc>
        public void Remove(Uri uriPrefix, string authenticationType)
        {
            if (uriPrefix == null || authenticationType == null)
            {
                // These couldn't possibly have been inserted into
                // the cache because of the test in Add().
                return;
            }

            ++_version;

            CredentialKey key = new CredentialKey(uriPrefix, authenticationType);

            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Print("CredentialCache::Remove() Removing key:[" + key.ToString() + "]");
            }

            _cache.Remove(key);
        }
Пример #7
0
        public override bool Equals(object comparand)
        {
            CredentialKey comparedCredentialKey = comparand as CredentialKey;

            if (comparand == null)
            {
                //
                // this covers also the compared==null case
                //
                return(false);
            }

            bool equals =
                AuthenticationType.Equals(comparedCredentialKey.AuthenticationType) &&
                UriPrefix.Equals(comparedCredentialKey.UriPrefix);

            GlobalLog.Print("CredentialKey::Equals(" + ToString() + ", " + comparedCredentialKey.ToString() + ") returns " + equals.ToString());

            return(equals);
        }
Пример #8
0
        /// <devdoc>
        ///  <para>
        ///    Returns the <see cref='System.Net.NetworkCredential'/>
        ///    instance associated with the supplied Uri and
        ///    authentication type.
        ///  </para>
        /// </devdoc>
        public NetworkCredential GetCredential(Uri uriPrefix, string authType)
        {
            if (uriPrefix == null)
            {
                throw new ArgumentNullException("uriPrefix");
            }
            if (authType == null)
            {
                throw new ArgumentNullException("authType");
            }

            GlobalLog.Print("CredentialCache::GetCredential(uriPrefix=\"" + uriPrefix + "\", authType=\"" + authType + "\")");

            int longestMatchPrefix = -1;
            NetworkCredential     mostSpecificMatch = null;
            IDictionaryEnumerator credEnum          = _cache.GetEnumerator();

            // Enumerate through every credential in the cache
            while (credEnum.MoveNext())
            {
                CredentialKey key = (CredentialKey)credEnum.Key;

                // Determine if this credential is applicable to the current Uri/AuthType
                if (key.Match(uriPrefix, authType))
                {
                    int prefixLen = key.UriPrefixLength;

                    // 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  = (NetworkCredential)credEnum.Value;
                    }
                }
            }

            GlobalLog.Print("CredentialCache::GetCredential returning " + ((mostSpecificMatch == null) ? "null" : "(" + mostSpecificMatch.UserName + ":" + mostSpecificMatch.Domain + ")"));

            return(mostSpecificMatch);
        }
Пример #9
0
        /// <devdoc>
        ///  <para>
        ///    Removes a <see cref='System.Net.NetworkCredential'/> instance from the credential cache.
        ///  </para>
        /// </devdoc>
        public void Remove(Uri uriPrefix, string authType)
        {
            if (uriPrefix == null || authType == null)
            {
                // These couldn't possibly have been inserted into
                // the cache because of the test in Add().
                return;
            }

            ++_version;

            CredentialKey key = new CredentialKey(uriPrefix, authType);

            GlobalLog.Print("CredentialCache::Remove() Removing key:[" + key.ToString() + "]");

            if (_cache[key] is SystemNetworkCredential)
            {
                --_numbDefaultCredInCache;
            }
            _cache.Remove(key);
        }
 public void Add(Uri uriPrefix, string authType, NetworkCredential cred)
 {
     if (uriPrefix == null)
     {
         throw new ArgumentNullException("uriPrefix");
     }
     if (authType == null)
     {
         throw new ArgumentNullException("authType");
     }
     if ((((cred is SystemNetworkCredential) && (string.Compare(authType, "NTLM", StringComparison.OrdinalIgnoreCase) != 0)) && (!DigestClient.WDigestAvailable || (string.Compare(authType, "Digest", StringComparison.OrdinalIgnoreCase) != 0))) && ((string.Compare(authType, "Kerberos", StringComparison.OrdinalIgnoreCase) != 0) && (string.Compare(authType, "Negotiate", StringComparison.OrdinalIgnoreCase) != 0)))
     {
         throw new ArgumentException(SR.GetString("net_nodefaultcreds", new object[] { authType }), "authType");
     }
     this.m_version++;
     CredentialKey key = new CredentialKey(uriPrefix, authType);
     this.cache.Add(key, cred);
     if (cred is SystemNetworkCredential)
     {
         this.m_NumbDefaultCredInCache++;
     }
 }
Пример #11
0
        public void Add(Uri uriPrefix, string authType, NetworkCredential cred)
        {
            if (uriPrefix == null)
            {
                throw new ArgumentNullException("uriPrefix");
            }
            if (authType == null)
            {
                throw new ArgumentNullException("authType");
            }
            if ((((cred is SystemNetworkCredential) && (string.Compare(authType, "NTLM", StringComparison.OrdinalIgnoreCase) != 0)) && (!DigestClient.WDigestAvailable || (string.Compare(authType, "Digest", StringComparison.OrdinalIgnoreCase) != 0))) && ((string.Compare(authType, "Kerberos", StringComparison.OrdinalIgnoreCase) != 0) && (string.Compare(authType, "Negotiate", StringComparison.OrdinalIgnoreCase) != 0)))
            {
                throw new ArgumentException(SR.GetString("net_nodefaultcreds", new object[] { authType }), "authType");
            }
            this.m_version++;
            CredentialKey key = new CredentialKey(uriPrefix, authType);

            this.cache.Add(key, cred);
            if (cred is SystemNetworkCredential)
            {
                this.m_NumbDefaultCredInCache++;
            }
        }
Пример #12
0
        /// <devdoc>
        ///  <para>
        ///    Adds a <see cref='System.Net.NetworkCredential'/> instance to the credential cache.
        ///  </para>
        /// </devdoc>
        public void Add(Uri uriPrefix, string authenticationType, NetworkCredential credential)
        {
            // Parameter validation
            if (uriPrefix == null)
            {
                throw new ArgumentNullException(nameof(uriPrefix));
            }
            if (authenticationType == null)
            {
                throw new ArgumentNullException(nameof(authenticationType));
            }

            ++_version;

            CredentialKey key = new CredentialKey(uriPrefix, authenticationType);

            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Print("CredentialCache::Add() Adding key:[" + key.ToString() + "], cred:[" + credential.Domain + "],[" + credential.UserName + "]");
            }

            _cache.Add(key, credential);
        }
Пример #13
0
        // properties

        // methods

        /// <devdoc>
        /// <para>Adds a <see cref='System.Net.NetworkCredential'/>
        /// instance to the credential cache.</para>
        /// </devdoc>
        // UEUE
        public void Add(Uri uriPrefix, string authType, NetworkCredential cred)
        {
            //
            // parameter validation
            //
            if (uriPrefix == null)
            {
                throw new ArgumentNullException("uriPrefix");
            }
            if (authType == null)
            {
                throw new ArgumentNullException("authType");
            }
            if ((cred is SystemNetworkCredential)
#if !FEATURE_PAL
                && !((string.Compare(authType, NtlmClient.AuthType, StringComparison.OrdinalIgnoreCase) == 0) ||
                     (DigestClient.WDigestAvailable && (string.Compare(authType, DigestClient.AuthType, StringComparison.OrdinalIgnoreCase) == 0)) ||
                     (string.Compare(authType, KerberosClient.AuthType, StringComparison.OrdinalIgnoreCase) == 0) ||
                     (string.Compare(authType, NegotiateClient.AuthType, StringComparison.OrdinalIgnoreCase) == 0))
#endif
                )
            {
                throw new ArgumentException(SR.GetString(SR.net_nodefaultcreds, authType), "authType");
            }

            ++m_version;

            CredentialKey key = new CredentialKey(uriPrefix, authType);

            GlobalLog.Print("CredentialCache::Add() Adding key:[" + key.ToString() + "], cred:[" + cred.Domain + "],[" + cred.UserName + "]");

            cache.Add(key, cred);
            if (cred is SystemNetworkCredential)
            {
                ++m_NumbDefaultCredInCache;
            }
        }
Пример #14
0
        /// <devdoc>
        ///  <para>
        ///    Adds a <see cref='System.Net.NetworkCredential'/> instance to the credential cache.
        ///  </para>
        /// </devdoc>
        public void Add(Uri uriPrefix, string authType, NetworkCredential cred)
        {
            // Parameter validation
            if (uriPrefix == null)
            {
                throw new ArgumentNullException("uriPrefix");
            }
            if (authType == null)
            {
                throw new ArgumentNullException("authType");
            }

            ++_version;

            CredentialKey key = new CredentialKey(uriPrefix, authType);

            GlobalLog.Print("CredentialCache::Add() Adding key:[" + key.ToString() + "], cred:[" + cred.Domain + "],[" + cred.UserName + "]");

            _cache.Add(key, cred);
            if (cred is SystemNetworkCredential)
            {
                ++_numbDefaultCredInCache;
            }
        }
Пример #15
0
        /// <devdoc>
        ///  <para>
        ///    Adds a <see cref='System.Net.NetworkCredential'/> instance to the credential cache.
        ///  </para>
        /// </devdoc>
        public void Add(Uri uriPrefix, string authenticationType, NetworkCredential credential)
        {
            // Parameter validation
            if (uriPrefix == null)
            {
                throw new ArgumentNullException("uriPrefix");
            }
            if (authenticationType == null)
            {
                throw new ArgumentNullException("authenticationType");
            }

            ++_version;

            CredentialKey key = new CredentialKey(uriPrefix, authenticationType);

            GlobalLog.Print("CredentialCache::Add() Adding key:[" + key.ToString() + "], cred:[" + credential.Domain + "],[" + credential.UserName + "]");

            _cache.Add(key, credential);
            if (credential is SystemNetworkCredential)
            {
                ++_numbDefaultCredInCache;
            }
        }
Пример #16
0
        public void Add(Uri uriPrefix, string authType, NetworkCredential cred)
        {
            if (uriPrefix == null)
            {
                throw new ArgumentNullException(nameof(uriPrefix));
            }
            if (authType == null)
            {
                throw new ArgumentNullException(nameof(authType));
            }

            if ((cred is SystemNetworkCredential) &&
                !((string.Equals(authType, NegotiationInfoClass.NTLM, StringComparison.OrdinalIgnoreCase)) ||
                  (string.Equals(authType, NegotiationInfoClass.Kerberos, StringComparison.OrdinalIgnoreCase)) ||
                  (string.Equals(authType, NegotiationInfoClass.Negotiate, StringComparison.OrdinalIgnoreCase)))
                )
            {
                throw new ArgumentException(SR.Format(SR.net_nodefaultcreds, authType), nameof(authType));
            }

            ++_version;

            var key = new CredentialKey(uriPrefix, authType);

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(this, $"Adding key:[{key}], cred:[{cred.Domain}],[{cred.UserName}]");
            }

            if (_cache == null)
            {
                _cache = new Dictionary <CredentialKey, NetworkCredential>();
            }

            _cache.Add(key, cred);
        }
 public void Remove(Uri uriPrefix, string authType)
 {
     if ((uriPrefix != null) && (authType != null))
     {
         this.m_version++;
         CredentialKey key = new CredentialKey(uriPrefix, authType);
         if (this.cache[key] is SystemNetworkCredential)
         {
             this.m_NumbDefaultCredInCache--;
         }
         this.cache.Remove(key);
     }
 }
Пример #18
0
        /// <devdoc>
        /// <para>Removes a <see cref='System.Net.NetworkCredential'/>
        /// instance from the credential cache.</para>
        /// </devdoc>
        public void Remove(Uri uriPrefix, string authType) {
            if (uriPrefix==null || authType==null) {
                // these couldn't possibly have been inserted into
                // the cache because of the test in Add()
                return;
            }

            ++m_version;

            CredentialKey key = new CredentialKey(uriPrefix, authType);

            GlobalLog.Print("CredentialCache::Remove() Removing key:[" + key.ToString() + "]");

            if (cache[key] is SystemNetworkCredential) {
                --m_NumbDefaultCredInCache;
            }
            cache.Remove(key);
        }