/// <summary> /// Returns user name, password and domain for the current favorite in the form of an object. /// </summary> /// <remarks>If neither a credential has been specified in the connection manual or /// no credential set from the XML file (credentials.xml) has been choosen, the defaults /// specified in the settings will be returned i.e. the default password, user name and domain. /// If the user has choosen not to use any of the options: /// * credential set /// * manual credential entry per favorite /// * default credentials in settings /// <c>NULL</c> values will be returned for each property in the credential object. /// </remarks> public static Credential GetCredentials(FavoriteConfigurationElement favorite) { Credential credential = new Credential { DomainName = string.IsNullOrEmpty(favorite.DomainName) ? string.IsNullOrEmpty(Settings.DefaultDomain) ? null : Settings.DefaultDomain : favorite.DomainName, UserName = string.IsNullOrEmpty(favorite.UserName) ? string.IsNullOrEmpty(Settings.DefaultUsername) ? null : Settings.DefaultUsername : favorite.UserName, Password = string.IsNullOrEmpty(favorite.Password) ? string.IsNullOrEmpty(Settings.DefaultPassword) ? null : Settings.DefaultPassword : favorite.Password, IsSetEncryptedPassword = !string.IsNullOrEmpty(favorite.EncryptedPassword) }; return credential; }
public static string ParseValue(Credential credential, string value) { if (string.IsNullOrEmpty(value) || credential == null) return value; string output = value; // Get the week number System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture; string weekNo = ci.Calendar.GetWeekOfYear(DateTime.Now, ci.DateTimeFormat.CalendarWeekRule, ci.DateTimeFormat.FirstDayOfWeek).ToString(); // loop five times and replace a maximum of 5 occurences per FIELDCONSTANT for (int i = 0; i < 5; i++) { output = ReplaceEx(output, ParsingConstants.WeekNumber, weekNo); output = ReplaceEx(output, ParsingConstants.UserName, credential.UserName); output = ReplaceEx(output, ParsingConstants.Password, credential.Password); output = ReplaceEx(output, ParsingConstants.DomainName, credential.DomainName); output = ReplaceEx(output, ParsingConstants.UserNameWithDomain, credential.UserNameWithDomain); output = ReplaceEx(output, ParsingConstants.DateTime, DateTime.Now.ToString()); // date time feature int startIndex = output.IndexOf(ParsingConstants.ParserStart + ParsingConstants.DateTimeMiddle + ParsingConstants.ParserSeperator); if (startIndex > -1) { // Unable to pare logic incorrect if (output.IndexOf(ParsingConstants.ParserEnd, startIndex) < output.IndexOf(ParsingConstants.ParserStart, startIndex)) continue; int start = startIndex + (ParsingConstants.ParserStart + ParsingConstants.DateTimeMiddle + ParsingConstants.ParserSeperator).Length; int end = output.IndexOf(ParsingConstants.ParserEnd, startIndex); // Parsing error if (start < 0 || end < 0) { continue; } // get the date time format string string format = output.Substring(start, end - start); // format the date time string output = ReplaceEx(output, ParsingConstants.ParserStart + ParsingConstants.DateTimeMiddle + ParsingConstants.ParserSeperator + format + ParsingConstants.ParserEnd, DateTime.Now.ToString(format)); } } return output; }