none
C# PrincipalSearcher filtering by LastPasswordSet RRS feed

  • Question

  • I am having trouble coming up with how to run a query and setting up the query by example to find users whose password will expire in X amount of days. 

    I can easily get data from the searcher already. Is there a way to do this in the query or is it something I would have to do after I've already gotten data back?

    I seems lastpasswordset is a get only and you can't query by it.
    • Edited by Zenuke Friday, November 11, 2016 2:46 PM
    Friday, November 11, 2016 2:42 PM

Answers

  • Hi Zenuke,

    >>Is there a way to do this in the query or is it something I would have to do after I've already gotten data back

    Actually, there is no Class or method to help you query directly in .Net Framework.

    >>I seems lastpasswordset is a get only and you can't query by it.

    I would suggest you use Linq to query result.

    For example, here is a sample about using Linq to query Users. We can filter conditions of the specified queries which you want behind Where statement.

    public static Task<IEnumerable<SomeUserModelClass>> GetUsers(//Whatever filters you want)
    {
        return Task.Run(() =>
        {
            PrincipalContext context = new PrincipalContext(ContextType.Domain);
            UserPrincipal principal = new UserPrincipal(context);
            principal.Enabled = true;
            PrincipalSearcher searcher = new PrincipalSearcher(principal);
    
            var users = searcher.FindAll().Cast<UserPrincipal>()
                .Where(x => x.SomeProperty... // Perform queries)
                .Select(x => new SomeUserModelClass
                {
                    userName = x.SamAccountName,
                    email = x.UserPrincipalName,
                    guid = x.Guid.Value
                }).OrderBy(x => x.userName).AsEnumerable();
    
            return users;
        });
    }

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, November 14, 2016 9:02 AM
    Moderator

All replies

  • Hi Zenuke,

    >>Is there a way to do this in the query or is it something I would have to do after I've already gotten data back

    Actually, there is no Class or method to help you query directly in .Net Framework.

    >>I seems lastpasswordset is a get only and you can't query by it.

    I would suggest you use Linq to query result.

    For example, here is a sample about using Linq to query Users. We can filter conditions of the specified queries which you want behind Where statement.

    public static Task<IEnumerable<SomeUserModelClass>> GetUsers(//Whatever filters you want)
    {
        return Task.Run(() =>
        {
            PrincipalContext context = new PrincipalContext(ContextType.Domain);
            UserPrincipal principal = new UserPrincipal(context);
            principal.Enabled = true;
            PrincipalSearcher searcher = new PrincipalSearcher(principal);
    
            var users = searcher.FindAll().Cast<UserPrincipal>()
                .Where(x => x.SomeProperty... // Perform queries)
                .Select(x => new SomeUserModelClass
                {
                    userName = x.SamAccountName,
                    email = x.UserPrincipalName,
                    guid = x.Guid.Value
                }).OrderBy(x => x.userName).AsEnumerable();
    
            return users;
        });
    }

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, November 14, 2016 9:02 AM
    Moderator
  • Hi Zenuke,

    If your issue is solved please Mark as answer or Vote as helpful post to the appropriate answer so that it will help other members if they faces similar issue.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Wednesday, November 16, 2016 9:39 AM
    Moderator