Visual Guard Class Library Documentation

How to integrate Visual Guard in WPF application

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

Referencing Visual Guard assemblies

In order to use Visual Guard, you must add references to Visual Guard assemblies:

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.

Add Visual Guard assemblies

If the assemblies does not appear in this list you can use the Browse tab and select them in the directory <Visual Guard installation directory> Visual Guard Console

Note: Once the Visual Guard assemblies are referenced into project, you need to mark "Copy Local" property to "true" for each assembly.

Description of Visual Guard assemblies:

 

Adding Visual Guard in your code

There are 1 main classes in Visual Guard:


You must load the security before all other code. In this case you will be sure to set the security of all the objects of your application. Visual Guard will not automatically set the security of this windows. In this case, you must set the security of this window after loading the permissions of the user (see the method VGSecurityManager.SetSecurity).

 

Call the login windows in the App.xaml

In the xaml of the App.xaml file you have to change the startup. You have to the event "Application_Startup".

<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">

 

In the code behind, in the event Application_startup you have to call the login page(1).

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

The following example demonstrates how to authenticate a user. This code can be inserted in the click event of the OK button in a login form (1):

[C#]
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

The following example demonstrates how to authenticate a user with Windows account. This code have to be inserted in the Application_startup event (1):

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

To secure Window class in your application, you must do the following:

 

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

See Also

Debugging Visual Guard Security Actions
How to apply security in your application
How Visual Guard secures an application
How to generate or edit the Visual Guard configuration files

How to integrate Visual Guard in WinForm applications
How to integrate Visual Guard in ASP.net applications
How to integrate Visual Guard in WCF applications
How to integrate Visual Guard in WCF / Silverlight services

 

(1) Change the code accordingly to control properties in your form