How to operate security entities (users, roles, and permissions) in ls )! Accessing security entities in a lightswitch Application

Source: Internet
Author: User

Accessing security entities in a lightswitch Application

Http://dearmusings.wordpress.com/2011/04/22/accessing-security-entities-in-a-lightswitch-application/

(Description: the entire discussion process in this article comes from the forum post: lightswitch design suggestion-being able to filter data by 'roles ')

Http://social.msdn.microsoft.com/Forums/en-SG/lightswitchgeneral/thread/7cbe5fc6-78db-47f6-8daf-a172604a1f33

Another post: lightswitch and business logic (how to establish the computing business logic on the server side !) ---- Use a service table to indicate the operations to be performed!

Http://social.msdn.microsoft.com/Forums/en-US/lightswitchgeneral/thread/cdd54133-2e33-44a1-b71b-7d4bc768dfe1

========================================================== ====================

Microsoft Visual Studio lightswitch beta helps you solve specific business needs by enabling you to quickly create professional-quality business applications, regardless of your development skills. lightswitch is a new addition to the Visual Studio family.

Lightswitch offers a great way to create business applications quickly. It offers functions alities out of the box including creating users & roles automatically.

However there are times when we need to create new user/roles programmatically.
One example cocould be if your application needs to assign say 1000 users to a single role doing this manually will be tedious task. you might want to do this via Code so as to do it with login.

This post will help you achieve exactly that.
There is already a very nice post by Matt that tells us how we can access ls security entities using RIA services

Here (how to reference security entities in lightswitch how to reference security entities in LS, use the wcf ria method !)

However with Matt's help I was able to create a new user, a New Role & assign the user to the newly created role all by using code

Here is my example

try
{
//Create a new role
Microsoft.LightSwitch.Security.Role newRole = this.DataWorkspace.SecurityData.Roles.AddNew();
newRole.Name = "NewRole";

// Save changes
This. dataworkspace. securitydata. savechanges ();

// Create a new user
Microsoft. lightswitch. Security. userregistration newuser = This. dataworkspace. securitydata. userregistrations. addnew ();
Newuser. Username = "testuser ";
Newuser. fullname = "Full name ";
Newuser. Password = "passw0rd @ 1 ";
// Save changes
This. dataworkspace. securitydata. savechanges ();

// Create new role assignment
Microsoft. lightswitch. Security. roleassignment Ra = This. dataworkspace. securitydata. roleassignments. addnew ();
RA. User = newuser;
RA. Role = newrole;

// Save changes
This. dataworkspace. securitydata. savechanges ();

}
Catch (validationexception E)
{
Foreach (validationresult result in E. validationresults)
{
This. showmessagebox (result. Message );
}
}

The code is pretty straightforward I create a new role, a new user & assign the user with the Role & save it all to the security entities.

One thing you might notice that I am calling the savechanges () method 3 times at every step when I am creating a user, a New Role & also at the time of assignment.

Ideally one call to savechanges () shocould have done this all for us but I guess its a bug in ls that it dose not allow us to save changes in all 3 entities at one go. I will try & sort y ls Team about it.

Meanwhile this code works & helps us handle our business scenario very well.

P.s. The Catch Block looks after any validation exceptions & handles them in an appropriate way. This cocould be very handy in case of debugging when you need to know whats the exact error message.

Also for this code to work properly you need to configure your ls app to useSecurityadministrationPermission while debugging.
This can be set via properties-> Access Control-> grant securityadministration access for debug.

Hope this helps some one
If you have any suggestions or feedback Please write back in comments.

With inputs fromMatt thalman (Microsoft)

Happy coding.

~ Supreet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.