Purpose: Provide a step-by-step procedure on adding security to an existing application.
Adding Security to an Existing Application
For a new application, security may be easily included by using the StrataFrame Windows Application w/ Security template when creating the Visual Studio project. However, adding security to existing applications requires some manual configuration.
To add security to an existing application, follow the steps below:
- Add Security Reference - Add a reference to MicroFour StrataFrame Security within your Visual Studio project.
- Open your application's visual studio project.
- Within the solution explorer, ensure that the Show All Files option is selected.
- Right-Click on the References folder and select the Add Reference option.
- Select the MicroFour StrataFrame Security component from the list, and click OK.
- Add Security Namespaces - Add the security namespaces to AppMain.vb (Visual Basic) or program.cs (C#).
- Set Data Source Key - Define and set the Security Data Source Key within the SetDataSources() method of AppMain.vb or program.cs. For a detailed description on the data source key, including what it does and how to set it, refer to the Setting the Security Data Source Key help topic.
- Create and Show the Login Form - The application login form is launched within the AppMain.vb (program.cs) file. If desired, a custom form may be used instead of the default StrataFrame login form.
- ShowLoginAndInitMainForm() - The login form must be launched within the ShowLoginAndInitMainForm() method of AppMain.vb or program.cs. For more information on configuring the ShowLoginAndInitMainForm() method, refer to the Showing the Initial Login Form topic.
- Custom Login Form - If a custom login form is desired, it must be created as a new SF Login Form item within the Visual Studio project. For more information on creating a custom login form, refer to the Creating a Custom Login Form topic.
- Configure the InitApplication() Method - The following items are added to the InitiApplication() method within the AppMain.vb or program.cs file to configure the remaining security options. The code snippits shown contain all default values for the listed options.
- Global Preferences - Required - Retrieve the global preferences from the database using the following method call. For more information, refer to the Retrieving Global Preferences from the Database help topic.
- Set Encryption Key - Required - Set the encryption key to be used for user data using the following method call. For more information, refer to the Specifying the Encryption Key for User Data help topic.
- Configure Session Locking - Possibly Required - Start the session locking monitor and configure the quick lock key using the following lines of code. This is only required if dynamic session locking will be used within your application. For more information, refer to the Initializing Session Locking help topic.
- Specify Initial Usernames and Passwords - Optional - Custom users for administration and security maintenance may be created using the code below.
- Specify Default Security Settings - Optional - The default permission action, blocked message (using plain text or a message key), replacement character, and replacement regex can be specified using the following assignments.
- Allow or Deny Windows Authentication - Optional - Windows authentication may be explicitly denied using the follwoing line of code. To allow windows authentication within the application, simply change the below False to a True.
Imports MicroFour.StrataFrame.Security Imports MicroFour.StrataFrame.Security.BusinessObjects
using MicroFour.StrataFrame.Security; using MicroFour.StrataFrame.Security.BusinessObjects;
'-- Set the data source key for the security tables SecurityBasics.SecurityDataSourceKey = ""
Private Shared Sub ShowLoginAndInitMainForm(ByVal e As ShowLoginAndInitFormEventArgs)
'-- Set the login form to your custom login form (optional)
'Login.LoginFormType = GetType(MyLoginForm)
'-- Show the login form and authenticate the user
e.ShowMainForm = Login.ShowLoginAndAuthUser(True)
End Sub'-- Retrieve the global preferences SFSPreferencesBO.RetrieveSecurityPreferences()
'-- Set the encryption key and vector for the user data
SecurityBasics.SetSecurityKeyAndVectorForUserAuthentication("MySecurityKey")'-- Start the session locking monitor & set the quick key to lock the application SessionLock.StartSessionMonitoring() SessionLock.SessionLockKey = Keys.F11
Note: These users will not appear to end-users within the Security Maintenance and cannot be configured outside of the below code assignments. As such, dynamic usernames and/or passwords (such as the default administrator password which dynamically includes the current date) are recommended.
'-- Set the administrative and security maintenance usernames and passwords SecurityBasics.AdministratorUserName = "Administrator" SecurityBasics.AdministratorPassword = "admin" & DateTime.Now.Day.ToString() '-- set the admin password so that it changes from day to day SecurityBasics.AdministratorUserPk = -1 SecurityBasics.SecurityMaintenanceUserName = "SecurityUser" SecurityBasics.SecurityMaintenancePassword = "mySecurityUserPass1" SecurityBasics.SecurityMaintenanceUserPk = -2
'-- Set the default actions for security enabled objects within the application
SecurityBasics.DefaultPermissionInfo = New PermissionInfo(PermissionAction.Deny, _
"Access Denied.", DeniedActions.Message)
SecurityBasics.DefaultPermissionAction = PermissionAction.Deny
SecurityBasics.DefaultBlockedMsg = "Access Denied."
'SecurityBasics.DefaultBlockedMsgKey = "AccessDeniedKey"
SecurityBasics.BlockedReplacementCharacter = "*"c
SecurityBasics.BlockedReplacementRegex = "[A-Za-z0-9@]"'-- Determine whether to allow Windows authentication SecurityBasics.AllowWindowsAuth = False