public static Dictionary<string, UserNameIdentityToken> LoadUserName(string applicationName)
{
Dictionary<string, UserNameIdentityToken> resultTokens = new Dictionary<string, UserNameIdentityToken>();
try
{
string relativePath = Utils.Format("%CommonApplicationData%\\OPC Foundation\\Accounts\\{0}", applicationName);
string absolutePath = Utils.GetAbsoluteDirectoryPath(relativePath, false, false, false);
if (string.IsNullOrEmpty(absolutePath))
{
return resultTokens;
}
foreach (string filePath in Directory.GetFiles(absolutePath))
{
// oops - nothing found.
if (filePath == null)
{
continue;
}
// open the file.
using (FileStream istrm = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
using (XmlTextReader reader = new XmlTextReader(istrm))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(UserNameIdentityToken));
UserNameIdentityToken userNameToken = (UserNameIdentityToken)serializer.ReadObject(reader, false);
if (userNameToken.UserName == null || userNameToken.Password == null)
{ // The configuration file has problem.
Utils.Trace("Unexpected error saving user configuration for COM Wrapper.");
continue;
}
if (resultTokens.ContainsKey(userNameToken.UserName))
{ // When I already exist, I ignore it.
Utils.Trace("When I already exist, I ignore it. UserName={0}", userNameToken.UserName);
continue;
}
userNameToken.Password = DecryptPassword(userNameToken.Password);
userNameToken.DecryptedPassword = new UTF8Encoding().GetString(userNameToken.Password);
resultTokens.Add(userNameToken.UserName, userNameToken);
}
}
}
}
catch (Exception e)
{
Utils.Trace(e, "Unexpected error saving user configuration for COM Wrapper.");
}
return resultTokens;
}