Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Manage Existing Users and Roles using a LightSwitch Application

0.00/5 (No votes)
28 Nov 2011 2  
This article presents a LightSwitch application that is used to manage users and roles from an existing database

user.png

Introduction

This article presents how you can use an existing users database with a LightSwitch application. I searched for an answer to this problem on the web, but I didn't find the answer that I was looking for. I found a sample about linking the current application users to another table but no sample about using an existing users database in a LightSwitch application. This article presents this process step by step.

Article Contents

  • Answers to questions
  • Creating the users database
  • Creating the LightSwitch application
  • Modifying the LightSwitch application to connect to the existing users database

Answers to Questions

What does your application/extension do? What business problem does it solve?

The application is used to manage the users in an existing database.

How many Screens and Entities does this Application Have?

The application has no entities and no custom screens. The application uses the default users and roles screens to show the functionality.

Did LightSwitch Save your Business Money? How?

This application could save money by making it possible to use existing user databases. There will be no need to reenter the users in the new LightSwitch app.

Creating the Users Database

LightSwitch applications use the same database tables for security that are used by all the other .NET applications. Since I do not have an existing users database, I will create one here.

As we all know, in order to add the aspnet_* database tables and stored procedures to an existing database, we can use the aspnet_regsql.exe tool. After we have created the database by using SQL Server Management Studio, we can run the following command in order to add our tables.

addTables.png

In the image above, you can see that we are adding the membership, role and profile tables to the LSTestDB database. These are the tables that are used by the LightSwitch application. After this command is run, we have the specified tables in our database. This can be seen in the image below:

addTables2.png

LightSwitch uses an additional table. This table is named RolePermissions and it is used to hold all the permissions assigned to the roles. The image below presents the table structure:

addTables3.png

We do not need to add this table to our database. The permissions will be held in the LightSwitch application database (that uses the _IntrinsicData connection string)

At this point, the database is ready to go.

Creating the LightSwitch Application

The next step is to create the LightSwitch application. For this, we just follow the new project wizard. After the project is opened, we need to set up the application to use Forms Authentication. This can be done from the Access Control tab in the Properties window. This can be seen in the image below:

accessControl.png

As you can see from the image above, I have granted the Security Administration permission for debug. This will allow us to access the corresponding screens in the UI. We can run the app now to see that everything is ok. The image below presents the application:

emptyApp.png

Remember not to add any users as this will add the users to the LightSwitch database.

Modifying the LightSwitch Application to Connect to the Existing Users Database

Now comes the interesting part. We will modify some of the LightSwitch generated files in order to connect to our users database. In particular, we will modify the generated web.config file. This file can be found in the ServerGenerated project. This is one of the projects that got generated when we created our application project. In order to get to the web.config file, we need to switch from Logical View to File View as in the image below:

fileView.png

We than need to click Show All Files and open the web.config file from the ServerGenerated project.

In this file, we can see a number of interesting things:

  • A connection string has been added that points to the default data source and its name is _IntrinsicData.
  • The membership, role and profile providers are set up and use the _IntrinsicData connection string.

We need to do two things in order to make the application use our users database:

  • Add a new connection string to our users database.
  • Make the providers use our connection string instead of _IntrinsicData.

We will add the following connection string to the web.config file:

<add name="myDB" connectionString="Data Source=.\sqlexpress;
         Initial Catalog=LSTestDB;Integrated Security=True;
         Connect Timeout=30;MultipleActiveResultSets=True" />

This connection string is very similar to the generated one except that the User Instance property is not set. All that is left now is to point the providers to use our connection string. In case the users already exist in the database, we also need to set the applicationName in the providers to the application from which we want to access the users.

This is it. The first thing we need to do to start testing our work is to add some random permission like in the image below:

permission.png

After this, we can start our application and add a test user and role. This can be seen in the images below. Our custom permission is also added to the role.

role.png

user.png

The last thing we need to do is to check our database to see if the data has been added. The image below presents the results.

results.png

From the image above, you can see that the user and role were indeed added.

This is it. I hope you like the article. If you have time or another solution to this problem, please feel free to post a comment.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here