System.Net.CredentialCache.Add C# (CSharp) Method

Add() public method

public Add ( Uri uriPrefix, string authType, NetworkCredential cred ) : void
uriPrefix Uri
authType string
cred NetworkCredential
return void
        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;
            }
        }

Same methods

CredentialCache::Add ( string host, int port, string authenticationType, NetworkCredential credential ) : void

Usage Example

Example #1
1
        /// <summary>
        /// method to download the contents of a file from a remote URI
        /// </summary>
        /// <param name="ftpUri"></param>
        /// <param name="user"></param>
        /// <param name="pass"></param>
        /// <returns></returns>
        public static string GetFileFromSite(Uri ftpUri, string user, string pass)
        {
            string fileString = string.Empty;

            // The serverUri parameter should start with the ftp:// scheme.
            if (ftpUri.Scheme != Uri.UriSchemeFtp)
            {
                return string.Empty;
            }
            // Get the object used to communicate with the server.
            WebClient request = new WebClient();

            // This example assumes the FTP site uses anonymous logon.
            NetworkCredential nc = new NetworkCredential(user, pass);
            CredentialCache cc = new CredentialCache();
            cc.Add(ftpUri, "Basic", nc);
            request.Credentials = cc;

            try
            {

                byte[] newFileData = request.DownloadData(ftpUri.ToString());
                fileString = System.Text.Encoding.UTF8.GetString(newFileData);
            }
            catch (WebException e)
            {
                m_logger.WriteToLogFile("FtpUtils::GetFileFromSite();ECaught: " + e.Message);
            }
            return fileString;
        }
All Usage Examples Of System.Net.CredentialCache::Add