LS2013 Core Project – Create the security data source

This section will have you scratching your head. I’ll show you how to add a data source to your project that will allow you to manage the LightSwitch security data, including users, roles and permissions. You won’t have to use Web API or anything that’s not within the LightSwitch environment. Its pretty slick!

So if you will, think back to the Silverlight client. It communicates to the server via OData. Including its security and user management. Hmmmm… you say. Yep. We’ve taken this concept and have worked the process so that we can do this same security management only with the HTML Client.

  1. In Visual Studio
  2. Go ahead and make sure your solution has been saved
  3. Do a full solution build
  4. Run your app, remember the port number
  5. Back in Visual Studio
  6. Right click on Data Sources
  7. Select Add Data Source
  8. Select OData Service
  9. Your OData endpoint will be:
  10. http://localhost:{port}/Microsoft.LightSwitch.SecurityData.svc
  11. Uncheck the Attach as read-only box
  12. Under Authentication Type, select Other
  13. User name will be the Security Administrator, LeaveMeAlone
  14. Password will be whatever you had set, just4fun!
  15. Select all the entities
  16. Name the data source, LsSecurityData
  17. Click finish
  18. Permissions should load into the editor
  19. Select the App tab at the bottom of the screen
  20. Change its Summary Property to Name
  21. Save your solution
  22. Add a new query to the UserRegistrations table
  23. Name it: CurrentUser
  24. Edit Additional query code
  25. Add the following into the method
    query = query
         .Where(u => u.UserName == this.Application.User.Name)
         .Select(u => u);
    
  26. Add another query to the UserRegistrations table
  27. Name it: NonSecurityAdministrators
  28. SortBy FullName
  29. Edit Additional Query Code
  30. Add the following into the method
    // Get a list of all the Security Admins
    var ra = RoleAssignments
    			.Where(a => a.RoleName == "Administrator")
    			.Select(a => new { a.SourceAccountName })
    			.Execute()
    			.ToList();
    
    // Create a nice delimited string... query does not like lists
    var listOfAdmins = ra.Aggregate("", 
    (current, u) => current + (u.SourceAccountName + ";"));
    
    // Update the query to exclude the 
    // list of Security Administrators
    query = query
    	.Where(u => !listOfAdmins.Contains(u.UserName))
    	.Select(u => u);
    
    			

Now don’t worry, we’ll show you how to deploy successfully a little later

Now… lets go create some screens!

Next… Screens For Permissions


Here are the topics and their logical order:

  1. Create the project
  2. Add external CSS
  3. Add external scripts
  4. Update the default.htm
  5. Add LogIn/LogOut/Register/ChangePassword
  6. — You
  7. Screens for permissions
  8. Screens for role management
  9. Screens for user management
  10. Tables for tile menus
  11. Screens for icon management
  12. Screens for menu management
  13. Create a tile menu screen
  14. Create a database project
  15. How to deploy successfully
  16. Wrap up

10 thoughts on “LS2013 Core Project – Create the security data source

  1. Roberto says:

    Thank you! You saved my life.

  2. Michael Collazo says:

    Converted code in Line 30 to VB.Net using the converter at http://converter.telerik.com/.

    Here is the result:

    ‘ Get a list of all the Security Admins
    Dim ra = RoleAssignments.Where(Function(a) a.RoleName = “Administrator”).[Select](Function(a) New From { _
    a.SourceAccountName _
    }).Execute().ToList()

    ‘ Create a nice delimited string… query does not like lists
    Dim listOfAdmins = ra.Aggregate(“”, Function(current, u) current + (u.SourceAccountName + “;”))

    ‘ Update the query to exclude the
    ‘ list of Security Administrators
    query = query.Where(Function(u) Not listOfAdmins.Contains(u.UserName)).[Select](Function(u) u)

    ‘=======================================================
    ‘Service provided by Telerik (www.telerik.com)
    ‘Conversion powered by NRefactory.
    ‘Twitter: @telerik
    ‘Facebook: facebook.com/telerik
    ‘=======================================================

    I receive an error Here:

    ‘ Get a list of all the Security Admins
    Dim ra = RoleAssignments.Where(Function(a) a.RoleName = “Administrator”).[Select](Function(a) New From { _ ‘<-here is where I get the error….
    a.SourceAccountName _
    }).Execute().ToList()

    "The error is "Comma, ')', or a valid expression continuation expected."

    I don't know how reliable the converter is so I tried another and received the same error.

    Any help would be appreciated!

    Thanks in advance…

  3. Lerner says:

    Hi

    How should i update the url to the security data end point after deploiment?

    • dwm9100b says:

      Depends on how you deploy.

      If you use the wizard, under data connections, there will be a second tab, here is here it will ask for the endpoint.

      You can also edit the web.config with the same settings and result.

      dale

  4. Lerner says:

    Hi on my browseUsers screen and on my browseRoles screen i’m getting an error on the list control of users/roles that object reference is not set to an instance of an object.

    The same error is with the core project application that i downloaded from here:
    http://lightswitch.codewriting.tips/topics/#/ViewPage/214/213/%5Bdb38f8160%5D

    Thanks

    • dwm9100b says:

      Good day,

      You need to change the connection string to the security data source. When in development, visual studio will change the port number for app in iisExpress. So you can do a couple of things:

      Change the port in the web.config

      Change the port in the web.config, then change the project not to dynamically assign the port.

      dale

  5. Johann Alexander Quintero says:

    Thanks for this… is awesome. I’m following the post and doing the exercise but when I’m building the solution after I add the the oData Source i’m getting this error:

    Error 2 The type or namespace name ‘LsSecurityData’ could not be found in the global namespace (are you missing an assembly reference?) \\.Server\GeneratedArtifacts\DataServiceImplementation.cs 899 184 .Server

    Do you know what I’m missing?

    Thanks.

  6. Joe Lee says:

    The step 16 can not be the default name “SecurityData”, It must be changed.

  7. Joe Lee says:

    Anyone follow this post, make sure step is not the default name “SecurityData”. Use any other name should be fine.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: