locked
Query for Validation RRS feed

  • Question

  • Hello!!

    I’m developing an internal app for my company and I decided to give a try to lightswitch. The thing is that I’m trying to use the AD users list to do some validations in the app. I have an Entity called Revisor, which has a property called UserName. This UserName is the link with the AD. What I’m trying to do is to validate that the UserName corresponds to a valid user inside the AD.

    So far I have this:

    • I’ve created a dummy table Employee and a query associated with it (Employee By Name). This table won’t have any data, is just a dummy table.
    • I’ve created a method that given a name access the AD and retrieve all the users with that name. For example, imagine that make this call GetADUsersByName(“Carlos"), then the method will return a list with all users that his name contains Carlos.
    • I’ve modified the query preprocess like this:
    ///<summary></summary>
    /// Filter the users from the AD and retrieves those ones who match the given name.
    ///
    /// User Name.
    /// Users who match the given name.
    partial void EmployeesByName_PreprocessQuery(string Name, ref IQueryable<employee> query)</employee>
    {
        List<employee> employees = </employee>new List<employee>();</employee>
     
        if (!string.IsNullOrEmpty(Name) && Name.Length > 2)
        {
            var results = ActiveDirectoryHelper.GetADUsers(Name);
     
            foreach (var result in results)
            {
                employees.Add(new Employee
                {
                    DisplayName = result.Properties[ActiveDirectoryHelper.DISPLAYNAME].ToString(),
                    UserName = result.Properties[ActiveDirectoryHelper.NAME].ToString(),
                    Email = result.Properties[ActiveDirectoryHelper.EMAIL].ToString()
                });
            }
        }
     
        query = query.Concat(employees);
    }
    • And I’ve created the following validation for the UserName property in the Revisor table:
    ///<summary></summary>
    /// Validates that the UserName corresponds to a valid user in AD.
    ///
    /// Errors results.
    partial void UserName_Validate(EntityValidationResultsBuilder results)
    {
        if (!string.IsNullOrEmpty(this.UserName))
        {
            IEnumerable<employee> users =</employee>this.DataWorkspace.ApplicationData.EmployeesByName(this.UserName).Execute();
     
            if (users.Count() == 0)
            {
                results.AddPropertyError("The User Name does not match with a valid AD's user");
            }
        }
    }

    My problem is that when I call the query within the validation, no results are returned. If I trace the code, in the return statement of the query_preprocess, the collection has the correct elements, but when the call return in the validation, the collection has no elements.

    I’m new in lightswitch and I’m not familiar with the way that queries are handle. Do you have any ideas? Do you have other solution to validate the UserName?

    Thanks in advance!!

    Friday, February 15, 2013 11:10 AM

All replies

  • This isnt the first time you have asked this question is it.

    My question to you is why are you doing it this way? LightSwitch has a built in way of doing checks against AD by going into the properties of the project, select access control and use the Windows authentication within there.


    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    Friday, February 15, 2013 11:45 AM
  • Yes, I post it in another forum, maybe you see it there. The thing is that revisors has nothing to do with users that can log in or not. I give you another example. I have another table called Projects that have two properties, Team Leader and Technical Leader. Both fields have the name of my company employees and these names must be users inside AD.

    What Lightswitch provides is a way to use AD to log in and authorize users to operations, but I need to be able to ask AD directly.

    If anyone comes up with another way to do this please let me know!!!

    Friday, February 15, 2013 12:56 PM
  • I have an Entity called Revisor, which has a property called UserName. This UserName is the link with the AD. What I’m trying to do is to validate that the UserName corresponds to a valid user inside the AD.

    So what exactly are you checking for? Are you just trying to do an AD lookup on a particular username? If so then there are a few code examples of Querying AD from Lightswitch that you could use without going to the steps that you are


    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    Friday, February 15, 2013 1:54 PM