In this article we will discuss about the set up security to control user access to parts of a Visual Studio LightSwitch Application 2011.
First of all we create tables and their corresponding screens in the LightSwitch Application. For creating tables and screens in LightSwitch Application, Please visit this link "http://www.c-sharpcorner.com/1/274/visual-studio-lightswitch-2011.aspx".Step 1: Now go to properties.

Step 2: Click access control->Click user windows authentication->Click allow users specified in the users screen of your application->Write name, display name, description likes as a below image.

Step 3: Open EditableSortedProductsGrid screen->Select write code->Click EditableSortedProductsGrid_CanRun->Now write the below code.

using
System;
using
System.Linq;
using System.IO;
using
System.IO.IsolatedStorage;
using
System.Collections.Generic;
using
Microsoft.LightSwitch;
using
Microsoft.LightSwitch.Framework.Client;
using
Microsoft.LightSwitch.Presentation;
using
Microsoft.LightSwitch.Presentation.Extensions;
namespace
LightSwitchApplication
{
public partial
class Application
{
partial void
EditableSortedProductsGrid_CanRun(ref
bool result)
{
// Set result to the desired field value
result = this.User.HasPermission(Permissions.CanAccesProductScreen);
}
}
}
Step 4: Open customer table->Select write code->Click Customers_CanDelete, Customers_CanInsert, Customers_CanRead, Customers_CanUpdate->Now write the below code.

using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
Microsoft.LightSwitch;
using
Microsoft.LightSwitch.Security.Server;
namespace
LightSwitchApplication
{
public partial
class
ApplicationDataService
{
partial void
Customers_CanDelete(ref
bool result)
{
result = this.Application.HasPermission(Permissions.CanDeleteCustomer);
}
partial void
Customers_CanInsert(ref
bool result)
{
result = this.Application.HasPermission(Permissions.CanAddCustomer);
}
partial void
Customers_CanRead(ref
bool result)
{
result = this.Application.HasPermission(Permissions.CanViewCustomer);
}
partial void
Customers_CanUpdate(ref
bool result)
{
result = this.Application.HasPermission(Permissions.CanEditCustomer);
}
}
}
Step 5: Open SearchCustomer screen->Select write code->Click SearchCustomer_CanRun->Now write the below code.

using
System;
using
System.Linq;
using System.IO;
using
System.IO.IsolatedStorage;
using
System.Collections.Generic;
using
Microsoft.LightSwitch;
using
Microsoft.LightSwitch.Framework.Client;
using
Microsoft.LightSwitch.Presentation;
using
Microsoft.LightSwitch.Presentation.Extensions;
namespace
LightSwitchApplication
{
public partial
class Application
{
partial void
EditableSortedProductsGrid_CanRun(ref
bool result)
{
// Set result to the desired field value
result = this.User.HasPermission(Permissions.CanAccesProductScreen);
}
partial void
SearchCustomers_CanRun(ref
bool result)
{
// Set result to the desired field value
result = this.User.HasPermission(Permissions.CanViewCustomer);
}
}
}












Join the conversation! Your thoughts help the community grow.