How to integrate into WPF ?

Estimated reading: 5 minutes 758 views

To integrate Visual Guard in your WPF application you have to:

  • Add the assemblies of Visual Guard as references of your project.
  • Integrate Visual Guard in the code of your application.
  • Create a Visual Guard repository and declare your application by using the
    Visual Guard console. This repository will contain all security items (users,
    roles, permissions …) of your application.
  • Generate the Visual Guard configuration
    files by using the Visual Guard console. These configuration files will be
    needed to connect your application to the repository.
  • Grant read/write permissions to the repository.

Integration Demo

Referencing Visual Guard assemblies

  • Open the solution of your project in Visual Studio.
  • In the solution explorer, expand the project node.
  • Right-click the Project node for the project and select Add Reference from the shortcut menu.
  • In .Net tab, select the 4 assemblies
    1. Novalys.VisualGuard.Security
    2. Novalys.VisualGuard.Security.WinForm
    3. Novalys.VisualGuard.Security.<RepositoryType> (Files, SQLServer or Oracle)
    4. Novalys.VisualGuard.Security.<ApplicationFrameworkType> (Depending on type of application’s framework, whether .NetFramework or .NetCore)
    And, then click the Select button, and then click the OK button
Note Note
In the list of assemblies, Visual Studio can display different
versions of the Visual Guard assemblies. You must select the assembly
corresponding to the version of the framework and target (x64, x32, or Any CPU)
used in your project.

Note Note
Once the Visual Guard assemblies are referenced into project, you need to mark “Copy Local” property to “true” for each assembly.
Note Note
You must add either Novalys.VisualGuard.Security.NetFramework or Novalys.VisualGuard.Security.Core (Depending on type of application’s framework)
  • Novalys.VisualGuard.Security contains the main Visual Guard classes.
  • Novalys.VisualGuard.Security.Files contains the classes needed to access
    to a file based repository.
  • Novalys.VisualGuard.Security.SQLServer contains the classes
    needed to access to a repository stored in a Microsoft SQLServer database
    (SQLServer 2005 or higher). Available only in Visual Guard Enterprise Edition
  • Novalys.VisualGuard.Security.Oracle
    contains the classes needed to access to a repository stored in an Oracle
    database (8i or higher). Available only in Visual Guard Enterprise Edition
  • Novalys.VisualGuard.Security.WPF contains all classes based on WPF
    control. This assembly is needed only if you use the forms provided by Visual
    Guard to authenticate, change a password or select a role. If you want to use
    your own form you do not need to add a reference to this assembly.
  • Novalys.VisualGuard.Security.NetFramework contains all classes required to support .Net Framework applications.
    This assembly is needed only if you want to integrate Visual Guard in .net framework applications.
  • Novalys.VisualGuard.Security.Core contains all classes required to support .Net Core applications.
    This assembly is needed only if you want to integrate Visual Guard in .net core applications.

Adding Visual Guard in your code

  • Novalys.VisualGuard.SecurityVGSecurityManager: This class
    provides the main access point for interacting with Visual Guard. It
    provides authentication and authorization features, it allows to set
    the security of the object of your application.
  • Novalys.VisualGuard.Security.WPFVGLoginForm :
    This class is the default authentication form provided by Visual Guard. This
    class is very easy to use and is fully integrated with Visual Guard. You can
    use your own form, to authenticate a user. In this case you must call the Authenticate method provided by the VGSecurityManager class.

Call the login windows in the App.xaml

<Application x:Class="HowToWPF.App"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 Startup="Application_Startup">
using Novalys.VisualGuard.Security;
... 
private void Application_Startup(object sender, StartupEventArgs e)
{ 
             Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
             //Call the login Page
             VGLoginForm uLogin = new VGLoginForm(); 
             bool? result = uLogin.ShowDialog(); 
             Application.Current.MainWindow = null; 
             Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose; 
             if (result.Value == true) 
             { 
                          Window1 win = new Window1(); 
                          win.Show(); 
             } 
             else 
             { 
                          MessageBox.Show("Authentication Error"); 
                          Application.Current.Shutdown(1); 
             }
}


Authenticating a user with your own login window

using Novalys.VisualGuard.Security; 
VGAuthenticationState state = VGSecurityManager.Authenticate(txtUserName.Text, txtPassword.Password); 
if (state.IsFailed) 
{ 
            DialogResult = false; 
            if (state.IsCanceled) return; 
            if (state.IsCredentialInvalid) 
{ 
                        if (state.IsLastBadLogin) 
                        { 
                                    MessageBox.Show("Invalid user or password. The next bad login will lock your account.");
                        } 
                        else
                       { 
                                    MessageBox.Show("Invalid user or password"); 
                        }
            } 
            else if (state.IsUserNotAuthorized) 
            { 
                        MessageBox.Show("user not authorized to log on the application"); 
            } 
            else if (state.IsUserAccountExpired) 
            { 
                        MessageBox.Show("your account is no more valid. Contact your administrator"); 
            } 
            else if (state.IsUserAccountNotYetAvailable) 
            { 
                        MessageBox.Show("your account is not yet available."); 
            } 
            else if (state.IsUserAccountLockedOut)
            { 
                        MessageBox.Show("your account is locked. Contact your administrator."); 
            } 
            else if (state.MustChangePasswordAtNextLogon) 
            { 
                        MessageBox.Show("Your password is not secure enough. You must change it."); 
            } 
 } 
 else 
 { 
            DialogResult = true; 
            if (!state.IsPasswordSecure) 
            { 
                        MessageBox.Show("Your password is not secure enough. You must change it.");
            }
 }
using Novalys.VisualGuard.Security; 

VGAuthenticationState state = VGSecurityManager.Authenticate(txtUserName.Text, txtPassword.Password); 
if (state.IsFailed) 
{ 
           DialogResult = false; 
           if (state.IsCanceled) return; 
           if (state.IsCredentialInvalid) 
           { 
                      if (state.IsLastBadLogin) 
                      { 
                                 MessageBox.Show("Invalid user or password. The next bad login will lock your account."); 
                      } 
                      else 
                      { MessageBox.Show("Invalid user or password"); 
                      } 
           } 
           else if (state.IsUserNotAuthorized) 
           { 
                      MessageBox.Show("user not authorized to log on the application"); 
           } 
           else if (state.IsUserAccountExpired)
           { 
                      MessageBox.Show("your account is no more valid. Contact your administrator");
           } 
           else if (state.IsUserAccountNotYetAvailable)
           { 
           MessageBox.Show("your account is not yet available."); 
           }
           else if (state.IsUserAccountLockedOut)
           { 
           MessageBox.Show("your account is locked. Contact your administrator."); 
           }
           else if (state.MustChangePasswordAtNextLogon) 
           { 
           MessageBox.Show("Your password is not secure enough. You must change it."); 
           } 
} 
else 
{ 
           DialogResult = true; 
           if (!state.IsPasswordSecure) 
           { 
                      MessageBox.Show("Your password is not secure enough. You must change it."); 
           } 
}

Authenticating a user with Windows account

using Novalys.VisualGuard.Security; 
... 
VGAuthorizationState state = VGSecurityManager.LoadSecurity(System.Security.Principal.WindowsIdentity.GetCurrent()); 
if (state.IsFailed) 
{ 
          if (state.IsUserNotFound) 
          { 
                    MessageBox.Show("Your are not declared in the security repository"); 
          } 
          else if (state.IsUserNotAuthorized) 
          { 
                    MessageBox.Show("Your are not authorized to log on to this application"); 
          } 
} 
else 
{ 
          Window1 win = new Window1(); 
          win.Show(); 
}

Secure window class in your application

using Novalys.VisualGuard.Security; 
... 

public partial class Window1 : Window, VGISecurable 
{ 
          public Window1() 
          { 
                    InitializeComponent(); 
                    VGSecurityManager.SetSecurity(this); 
          }
}