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

Add() public method

public Add ( string host, int port, string authenticationType, NetworkCredential credential ) : void
host string
port int
authenticationType string
credential NetworkCredential
return void
        public void Add(string host, int port, string authenticationType, NetworkCredential credential) {
            //
            // parameter validation
            //
            if (host==null) {
                throw new ArgumentNullException("host");
            }

            if (authenticationType==null) {
                throw new ArgumentNullException("authenticationType");
            }
            
            if (host.Length == 0) {
                throw new ArgumentException(SR.GetString(SR.net_emptystringcall,"host"));
            }

            if (port < 0) {
                throw new ArgumentOutOfRangeException("port");
            }
            if ((credential is SystemNetworkCredential)
                ) {
                throw new ArgumentException(SR.GetString(SR.net_nodefaultcreds, authenticationType), "authenticationType");
            }

            ++m_version;

            CredentialHostKey key = new CredentialHostKey(host,port, authenticationType);

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

            cacheForHosts.Add(key, credential);
            if (credential is SystemNetworkCredential) {
                ++m_NumbDefaultCredInCache;
            }
        }

Same methods

CredentialCache::Add ( Uri uriPrefix, string authType, NetworkCredential cred ) : 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