Transformation.PowerShell.Common.AuthenticationHelper.GetSecureString C# (CSharp) Method

GetSecureString() public method

public GetSecureString ( string input ) : SecureString
input string
return System.Security.SecureString
        public SecureString GetSecureString(string input)
        {
            if (string.IsNullOrEmpty(input))
                throw new ArgumentException("Input string is empty and cannot be made into a SecureString", input);

            var secureString = new SecureString();
            foreach (char c in input.ToCharArray())
                secureString.AppendChar(c);

            return secureString;
        }

Usage Example

        public void DownloadFileFromHive(string absoluteFilePath, string outPutFolder, string SharePointOnline_OR_OnPremise = Constants.OnPremise, string UserName = "******", string Password = "******", string Domain = "NA")
        {
            bool headerCSVColumns = false;
            string exceptionCommentsInfo1 = string.Empty;

            GhostingAndUnGhosting_Initialization(outPutFolder, "DOWNLOAD");

            Logger.AddMessageToTraceLogFile(Constants.Logging, "############## DownloadFileFromHive - Trasnformation Utility Execution Started - For Web ##############");
            Console.WriteLine("############## DownloadFileFromHive - Trasnformation Utility Execution Started - For Web ##############");

            Logger.AddMessageToTraceLogFile(Constants.Logging, "[DATE TIME] " + Logger.CurrentDateTime());
            Console.WriteLine("[DATE TIME] " + Logger.CurrentDateTime());

            Logger.AddMessageToTraceLogFile(Constants.Logging, "[START] ::: DownloadFileFromHive");
            Console.WriteLine("[START] ::: DownloadFileFromHive");

            Logger.AddMessageToTraceLogFile(Constants.Logging, "[DownloadFileFromHive] Initiated Logger and Exception Class. Logger and Exception file will be available in path " + outPutFolder);
            Console.WriteLine("[DownloadFileFromHive] Initiated Logger and Exception Class. Logger and Exception file will be available in path" + outPutFolder);

            try
            {
                exceptionCommentsInfo1 = "FilePath: " + absoluteFilePath;
                string fileName = Path.GetFileName(absoluteFilePath);

                using (WebClient myWebClient = new WebClient())
                {
                    //SharePoint on-premises / SharePoint Online Dedicated => OP (On-Premises)
                    if (SharePointOnline_OR_OnPremise.ToUpper() == Constants.OnPremise)
                    {
                        myWebClient.Credentials = new System.Net.NetworkCredential(UserName, Password, Domain);
                    }
                    //SharePointOnline  => OL (Online)
                    else if (SharePointOnline_OR_OnPremise.ToUpper() == Constants.Online)
                    {
                        AuthenticationHelper ObjAuth = new AuthenticationHelper();
                        var spoPassword = ObjAuth.GetSecureString(Password);
                        myWebClient.Credentials = new SharePointOnlineCredentials(UserName, spoPassword);
                    }
                    myWebClient.Credentials = new System.Net.NetworkCredential(UserName, Password, Domain);
                    myWebClient.DownloadFile(absoluteFilePath, outPutFolder + "\\" + fileName);
                    Logger.AddMessageToTraceLogFile(Constants.Logging, "[DownloadFileFromHive] File is downloaded to the Directory : " + outPutFolder);
                    Console.WriteLine("[DownloadFileFromHive] File is downloaded to the Directory : " + outPutFolder);
                }

                DownloadFileBase objDFBase = new DownloadFileBase();
                objDFBase.GivenFilePath = absoluteFilePath;
                objDFBase.FileName = fileName;
                objDFBase.DownloadedFilePath = outPutFolder + "\\" + fileName;

                objDFBase.WebUrl = Constants.NotApplicable;
                objDFBase.SiteCollection = Constants.NotApplicable;
                objDFBase.WebApplication = Constants.NotApplicable;

                if (objDFBase != null)
                {
                    FileUtility.WriteCsVintoFile(outPutFolder + @"\" + Constants.UnGhosting_DownloadFileReport, objDFBase,
                        ref headerCSVColumns);
                }
            }
            catch (Exception ex)
            {
                Logger.AddMessageToTraceLogFile(Constants.Logging, "[Exception] DownloadFileFromHive. Exception Message: " + ex.Message);
                ExceptionCsv.WriteException(ExceptionCsv.WebApplication, ExceptionCsv.SiteCollection, ExceptionCsv.WebUrl, "DownloadFileFromHive", ex.Message, ex.ToString(), "DownloadFileFromHive", ex.GetType().ToString(), exceptionCommentsInfo1);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("[Exception] DownloadFileFromHive. Exception Message: " + ex.Message);
                Console.ForegroundColor = ConsoleColor.Gray;
            }

            Logger.AddMessageToTraceLogFile(Constants.Logging, "[END] ::: DownloadFileFromHive");
            Console.WriteLine("[END] ::: DownloadFileFromHive");

            Logger.AddMessageToTraceLogFile(Constants.Logging, "############## DownloadFileFromHive - Trasnformation Utility Execution Completed for Web ##############");
            Console.WriteLine("############## DownloadFileFromHive - Trasnformation Utility Execution Completed for Web ##############");
        }