Application Security, Part 23

Back in the code for the start-up of the primary form of the Smart Client,

 

static void Main(string[] asArguments)

                        {

                     Hashtable rArguments = null;

                    

                     try

                     {

                           rArguments = CUtility.rParseCommandLineArguments(asArguments,frmMain.c_sArgumentSeparator_Prefix,frmMain.c_sArgumentSeparator_Suffix);

                           string sCulture = (string)rArguments[frmMain.c_sArgumentKey_Culture];

                           if((sCulture != null)&&(sCulture != string.Empty))

                           {

                                  try

                                  {

                                         System.Globalization.CultureInfo rCulture = new System.Globalization.CultureInfo(sCulture);

                                         System.Threading.Thread.CurrentThread.CurrentCulture = rCulture;

                                         System.Threading.Thread.CurrentThread.CurrentUICulture = rCulture;

                                  }

                                  catch(Exception)

                                  {

                                  }

                           }

                           string sUserName = (string)rArguments[frmMain.c_sArgumentKey_UserName];

                           string sPassword = (string)rArguments[frmMain.c_sArgumentKey_Password];

                           System.Collections.Hashtable rConfigurationSection = (System.Collections.Hashtable)System.Configuration.ConfigurationSettings.GetConfig(frmMain.c_sConfigurationSection_Authorization);

                           byte byLimit_LoginAttempts = byte.Parse((string)rConfigurationSection[frmMain.c_sKey_Limit_LoginAttempt]);

                          

                           AuthenticationService.TaskVisionII_Authentication rAuthenticationService = new AuthenticationService.TaskVisionII_Authentication();

                           rAuthenticationService.Url = (string)rConfigurationSection[frmMain.c_sKey_Location_Service];

                           ICredentials rCredentials = null;

                          

                          

                           AuthenticationService.CUser rUser = null;

                           frmLogin rLogin = null;

                           byte cAttempts = 0;

                           while(cAttempts < byLimit_LoginAttempts)

                           {

                                  try

                                  {

                                         if((sUserName != null)&&(sPassword != null))

                                         {

                                                rCredentials = new System.Net.NetworkCredential(sUserName,sPassword);

                                         }

                                         else

                                         {

                                                rCredentials = System.Net.CredentialCache.DefaultCredentials;

                                         }

                                         rAuthenticationService.Credentials = rCredentials;

                                         rUser = rAuthenticationService.rAuthenticate();

                                         break;

                                  }

                                  catch(WebException rException)

                                  {

                                         if(rLogin == null)

                                         {

                                                rLogin = new frmLogin();

                                         }

                                        

                                         rLogin.Show();

                                         while(rLogin.Visible)

                                         {

                                                Application.DoEvents();

                                         }

                                         if(rLogin.DialogResult != DialogResult.OK)

                                         {

                                                break;

                                         }

                                         sUserName = rLogin.UserName;

                                         sPassword = rLogin.Password;

                                  }

                                  cAttempts++;

                           }

                          

                           if(rUser == null)

                           {

                                  throw new Exception();

                           }

                           Thread.CurrentPrincipal = new CUser(rUser.sName,rUser.sLanguage_Preferred,rUser.afPermissions,rCredentials,rUser.asRoles);

                          

                          

                     }

                     catch(Exception rException)

                     {

                           return;

                     }

                     fRestart = true;

                     while(fRestart)

                     {

                           fRestart = false;

                           try

                           {

                                  frmMain.SetCulture();

                           }

                           catch(Exception)

                           {

                           }

                          

                           Application.Run(new frmMain(rArguments));

                          

                     }

                    

                    

                    

              }

 

 

private static void SetCulture()

              {

                     try

                     {

                           string sPreferredLanguage = ((Utility.CUser)Thread.CurrentPrincipal).PreferredLanguage;

                           if((sPreferredLanguage != null)&&(sPreferredLanguage != string.Empty))

                           {

                                  System.Globalization.CultureInfo rCulture = new System.Globalization.CultureInfo(sPreferredLanguage);

                                  System.Threading.Thread.CurrentThread.CurrentCulture = rCulture;

                                  System.Threading.Thread.CurrentThread.CurrentUICulture = rCulture;

                                 

                           }

                     }

                     catch(Exception)

              {

                     }

              }

 

we see that the language preference retrieved from ADAM is used to set the culture for the currently-executing thread, which serves to determine the language of the text displayed on the user interface. The loop, 

                 

                  fRestart = true;

                     while(fRestart)

                     {

                           fRestart = false;

                           try

                           {

                                  frmMain.SetCulture();

                           }

                           catch(Exception)

                           {

                           }

                          

                           Application.Run(new frmMain(rArguments));

                          

                     }

 

in the static Main method of the form is rather interesting. Keep in mind that, typically, the Main method for the primary form of a Windows Forms application merely has a single line that passes an instance of the form to the Run method of the Application object, causing it to be displayed and have messages pumped to it by the operating system, like so:

 

Application.Run(new frmMain());

 

Our Main method has a version of that statement, but it is embedded in a loop. Here’s why: when a form is displayed, it uses the resources corresponding to the culture specified for the current thread, so the language of the text displayed on the form is determined at the point when the form is displayed. When a user elects to change his or her preferred language from the menu, we need to close and then redisplay the form for the change of language preference to take effect. Hence, we have this loop that will execute every time the user changes his or her language preference, thereby having the form display in the user’s chosen language. As an aside, the original version of TaskVision (https://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/wnf_taskvision.asp), which I don't like very much for a number of reasons (all of which, of course, are opinions of my own--you should form your own), and which I am rewriting in the course of these blog entries, required the user to shut down and restart the application for the change in the preferred language of the user interface to take effect.