ADRoleFactory.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WF / Activities / Role / ADRoleFactory.cs / 1305376 / ADRoleFactory.cs

                            #region Using directives 

using System;
using System.Collections.Generic;
using System.Text; 
using System.DirectoryServices;
using System.Security.Principal; 
using System.Configuration; 
using System.Workflow.Runtime.Configuration;
using System.Workflow.Activities.Configuration; 

#endregion

namespace System.Workflow.Activities 
{
    public static class ActiveDirectoryRoleFactory 
    { 
        private static DirectoryGroupQuery s_directoryGroupQuery = new DirectoryGroupQuery();
        private static String s_configurationSectionName = "System.Workflow.Runtime.Hosting.ADRoleFactory"; 
        private static ActiveDirectoryRoleFactoryConfiguration s_configuration;
        private static DirectoryEntry s_rootEntry;

        static ActiveDirectoryRoleFactory() 
        {
            s_configuration = (ActiveDirectoryRoleFactoryConfiguration)ConfigurationManager.GetSection(s_configurationSectionName); 
            if (s_configuration == null) 
                s_configuration = new ActiveDirectoryRoleFactoryConfiguration();
        } 


        public static ActiveDirectoryRole CreateFromAlias(String alias)
        { 
            if (alias == null)
                throw new ArgumentNullException("alias"); 
 
            ActiveDirectoryRole role = new ActiveDirectoryRole(GetRootEntry(), new DirectoryRootQuery("sAMAccountName", alias, DirectoryQueryOperation.Equal));
            role.Operations.Add(s_directoryGroupQuery); 
            ValidateRole(role);
            return role;
        }
 
        public static ActiveDirectoryRole CreateFromSecurityIdentifier(SecurityIdentifier sid)
        { 
            if (sid == null) 
                throw new ArgumentNullException("sid");
 
            ActiveDirectoryRole role = new ActiveDirectoryRole(GetRootEntry(), new DirectoryRootQuery("objectSID", sid.ToString(), DirectoryQueryOperation.Equal));
            role.Operations.Add(s_directoryGroupQuery);
            ValidateRole(role);
            return role; 
        }
 
        public static ActiveDirectoryRole CreateFromEmailAddress(String emailAddress) 
        {
            if (emailAddress == null) 
                throw new ArgumentNullException("emailAddress");

            ActiveDirectoryRole role = new ActiveDirectoryRole(GetRootEntry(), new DirectoryRootQuery("mail", emailAddress, DirectoryQueryOperation.Equal));
            role.Operations.Add(s_directoryGroupQuery); 
            ValidateRole(role);
            return role; 
        } 

        private static DirectoryEntry GetRootEntry() 
        {
            if (s_rootEntry == null)
            {
                if (s_configuration == null || 
                    s_configuration.RootPath == null ||
                    s_configuration.RootPath.Length == 0) 
                { 
                    s_rootEntry = new DirectoryEntry();
                } 
                else
                {
                    s_rootEntry = new DirectoryEntry(s_configuration.RootPath);
                } 
            }
 
            return s_rootEntry; 
        }
 
        public static ActiveDirectoryRoleFactoryConfiguration Configuration
        {
            get
            { 
                return s_configuration;
            } 
        } 

        private static void ValidateRole(ActiveDirectoryRole adRole) 
        {
            if (adRole.GetEntries().Count == 0)
                throw new ArgumentException(SR.GetString(SR.Error_NoMatchingActiveDirectoryEntry));
        } 

    } 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
#region Using directives 

using System;
using System.Collections.Generic;
using System.Text; 
using System.DirectoryServices;
using System.Security.Principal; 
using System.Configuration; 
using System.Workflow.Runtime.Configuration;
using System.Workflow.Activities.Configuration; 

#endregion

namespace System.Workflow.Activities 
{
    public static class ActiveDirectoryRoleFactory 
    { 
        private static DirectoryGroupQuery s_directoryGroupQuery = new DirectoryGroupQuery();
        private static String s_configurationSectionName = "System.Workflow.Runtime.Hosting.ADRoleFactory"; 
        private static ActiveDirectoryRoleFactoryConfiguration s_configuration;
        private static DirectoryEntry s_rootEntry;

        static ActiveDirectoryRoleFactory() 
        {
            s_configuration = (ActiveDirectoryRoleFactoryConfiguration)ConfigurationManager.GetSection(s_configurationSectionName); 
            if (s_configuration == null) 
                s_configuration = new ActiveDirectoryRoleFactoryConfiguration();
        } 


        public static ActiveDirectoryRole CreateFromAlias(String alias)
        { 
            if (alias == null)
                throw new ArgumentNullException("alias"); 
 
            ActiveDirectoryRole role = new ActiveDirectoryRole(GetRootEntry(), new DirectoryRootQuery("sAMAccountName", alias, DirectoryQueryOperation.Equal));
            role.Operations.Add(s_directoryGroupQuery); 
            ValidateRole(role);
            return role;
        }
 
        public static ActiveDirectoryRole CreateFromSecurityIdentifier(SecurityIdentifier sid)
        { 
            if (sid == null) 
                throw new ArgumentNullException("sid");
 
            ActiveDirectoryRole role = new ActiveDirectoryRole(GetRootEntry(), new DirectoryRootQuery("objectSID", sid.ToString(), DirectoryQueryOperation.Equal));
            role.Operations.Add(s_directoryGroupQuery);
            ValidateRole(role);
            return role; 
        }
 
        public static ActiveDirectoryRole CreateFromEmailAddress(String emailAddress) 
        {
            if (emailAddress == null) 
                throw new ArgumentNullException("emailAddress");

            ActiveDirectoryRole role = new ActiveDirectoryRole(GetRootEntry(), new DirectoryRootQuery("mail", emailAddress, DirectoryQueryOperation.Equal));
            role.Operations.Add(s_directoryGroupQuery); 
            ValidateRole(role);
            return role; 
        } 

        private static DirectoryEntry GetRootEntry() 
        {
            if (s_rootEntry == null)
            {
                if (s_configuration == null || 
                    s_configuration.RootPath == null ||
                    s_configuration.RootPath.Length == 0) 
                { 
                    s_rootEntry = new DirectoryEntry();
                } 
                else
                {
                    s_rootEntry = new DirectoryEntry(s_configuration.RootPath);
                } 
            }
 
            return s_rootEntry; 
        }
 
        public static ActiveDirectoryRoleFactoryConfiguration Configuration
        {
            get
            { 
                return s_configuration;
            } 
        } 

        private static void ValidateRole(ActiveDirectoryRole adRole) 
        {
            if (adRole.GetEntries().Count == 0)
                throw new ArgumentException(SR.GetString(SR.Error_NoMatchingActiveDirectoryEntry));
        } 

    } 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK