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.
- In Visual Studio
- Go ahead and make sure your solution has been saved
- Do a full solution build
- Run your app, remember the port number
- Back in Visual Studio
- Right click on Data Sources
- Select Add Data Source
- Select OData Service
- Your OData endpoint will be:
- http://localhost:{port}/Microsoft.LightSwitch.SecurityData.svc
- Uncheck the Attach as read-only box
- Under Authentication Type, select Other
- User name will be the Security Administrator, LeaveMeAlone
- Password will be whatever you had set, just4fun!
- Select all the entities
- Name the data source, LsSecurityData
- Click finish
- Permissions should load into the editor
- Select the App tab at the bottom of the screen
- Change its Summary Property to Name
- Save your solution
- Add a new query to the UserRegistrations table
- Name it: CurrentUser
- Edit Additional query code
- Add the following into the method
query = query .Where(u => u.UserName == this.Application.User.Name) .Select(u => u);
- Add another query to the UserRegistrations table
- Name it: NonSecurityAdministrators
- SortBy FullName
- Edit Additional Query Code
- 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:
- Create the project
- Add external CSS
- Add external scripts
- Update the default.htm
- Add LogIn/LogOut/Register/ChangePassword
- — You
- Screens for permissions
- Screens for role management
- Screens for user management
- Tables for tile menus
- Screens for icon management
- Screens for menu management
- Create a tile menu screen
- Create a database project
- How to deploy successfully
- Wrap up
Thank you! You saved my life.
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…
Hi
How should i update the url to the security data end point after deploiment?
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
Hi dwm9100b
Can you please explain me in more detail how to do this.
I’m deploying to “smarter asp.net”
So should i leave the url to localhost and only change the port number?
And how do i know the port number of the hosting server?
Right now the connection string in the web.config is: http://localhost:16549/Microsoft.LightSwitch.SecurityData.svc
Thanks
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
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
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.
The step 16 can not be the default name “SecurityData”, It must be changed.
Anyone follow this post, make sure step is not the default name “SecurityData”. Use any other name should be fine.