zoukankan      html  css  js  c++  java
  • 如何操作LS中的安全实体(用户、角色、权限)! Accessing security entities in a Lightswitch application (转)

         Accessing security entities in a Lightswitch application

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

        (说明:本文的整个讨论过程来自论坛的贴子:  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

       另一贴: LightSwitch and business logic. (如何建立服务器端的计算业务逻辑!)---- 使用一个服务表来表示需要执行的操作 !

    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 many functionalities out of the box including creating users & roles automatically.

        However there are times when we need to create new user/ roles programmatically.
    One example could 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 ease.

        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  如何引用安全实体在LS中,使用WCF RIA 方法!)

        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() should 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 & notify 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 could 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 use SecurityAdministration permission 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 from Matt Thalman (Microsoft)

    Happy Coding.

    ~Supreet

  • 相关阅读:
    C# DES加密和解密
    SQL设计技巧优化
    MS15-034漏洞技术研究
    Vustudy靶场环境快速搭建
    FastJson<=1.2.47漏洞复现
    多台电脑共享一套鼠键--Mouse Without Borders
    {Java初级系列四}---继承、接口和抽象类
    {Java初级系列三}---面向对象和类
    {Java初级系列二}---Java类基础知识
    {Java初阶系列一}---Java基本简介
  • 原文地址:https://www.cnblogs.com/zengxinle/p/2072710.html
Copyright © 2011-2022 走看看