コード例 #1
0
        public override string[] GetUsersInRole(string roleName)
        {
            var users = new List <string>();

            var directoryEntry = LdapManager.Connect(_ldapServer, _ldapPort,
                                                     _ldapUseSsl, _ldapGroupContainer, _ldapUserName, _ldapPassword, _ldapSimpleBind);

            var directorySearcher = new DirectorySearcher(directoryEntry);

            directorySearcher.Filter = String.Format("(&(ObjectClass=group)({0}={1}))",
                                                     _ldapGroupNameAttribute, roleName);
            directorySearcher.SearchScope = _ldapUserSearchScope;

            var result = directorySearcher.FindOne();

            if (result != null)
            {
                foreach (DirectoryEntry user in result.Properties["memberof"])
                {
                    users.Add(user.Properties[_ldapUserName].Value.ToString());
                }
                return(users.ToArray());
            }

            return(null);
        }
コード例 #2
0
        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            try
            {
                var directoryEntry = LdapManager.Connect(_ldapServer, _ldapPort,
                                                         _ldapUseSsl,
                                                         _ldapUserContainer,
                                                         _ldapUserName, _ldapPassword,
                                                         _ldapSimpleBind);

                if (!string.IsNullOrEmpty(_ldapUserName) && !string.IsNullOrEmpty(_ldapPassword))
                {
                    directoryEntry.Username = _ldapUserName;
                    directoryEntry.Password = _ldapPassword;
                }

                var directorySearcher = new DirectorySearcher(directoryEntry)
                {
                    Filter =
                        String.Format("(&(ObjectClass={0})({1}=*{2}*))",
                                      _ldapUserObjectClass,
                                      _ldapUserNameAttribute,
                                      providerUserKey),
                    SearchScope = _ldapUserSearchScope
                };

                var result = directorySearcher.FindOne();

                if (result != null)
                {
                    var user = GetUserFromSearchResult(result.GetDirectoryEntry());
                    return(user);
                }
            }
            catch (ActiveDirectoryServerDownException exception)
            {
                SPDiagnosticsService.Local.WriteTrace(100,
                                                      new SPDiagnosticsCategory("NaupliusADLDSProvider",
                                                                                TraceSeverity.High, EventSeverity.Error,
                                                                                0, 100), TraceSeverity.High,
                                                      "AD LDS Server is not responding " +
                                                      exception.StackTrace);
            }
            catch (Exception exception2)
            {
                SPDiagnosticsService.Local.WriteTrace(100,
                                                      new SPDiagnosticsCategory("NaupliusADLDSProvider",
                                                                                TraceSeverity.High, EventSeverity.Error,
                                                                                0, 100), TraceSeverity.High,
                                                      "Unexpected exception in GetUser(ob) " +
                                                      exception2.StackTrace);
            }

            return(null);
        }
コード例 #3
0
        public override string GetUserNameByEmail(string email)
        {
            try
            {
                var directoryEntry = LdapManager.Connect(_ldapServer, _ldapPort,
                                                         _ldapUseSsl,
                                                         _ldapUserContainer,
                                                         _ldapUserName, _ldapPassword,
                                                         _ldapSimpleBind);

                directoryEntry.Username = _ldapUserName;
                directoryEntry.Password = _ldapPassword;

                var directorySearcher = new DirectorySearcher(directoryEntry)
                {
                    Filter =
                        String.Format(
                            "(&(ObjectClass={0})(mail={1}))",
                            _ldapUserObjectClass, email),
                    SearchScope =
                        _ldapUserSearchScope
                };

                var result = directorySearcher.FindOne();

                if (result != null)
                {
                    var user = GetUserFromSearchResult(result.GetDirectoryEntry());
                    return(user.UserName);
                }
            }
            catch (ActiveDirectoryServerDownException exception)
            {
                SPDiagnosticsService.Local.WriteTrace(100,
                                                      new SPDiagnosticsCategory("NaupliusADLDSProvider",
                                                                                TraceSeverity.High, EventSeverity.Error,
                                                                                0, 100), TraceSeverity.High,
                                                      "AD LDS Server is not responding " +
                                                      exception.StackTrace);
            }
            catch (Exception exception2)
            {
                SPDiagnosticsService.Local.WriteTrace(100,
                                                      new SPDiagnosticsCategory("NaupliusADLDSProvider",
                                                                                TraceSeverity.High, EventSeverity.Error,
                                                                                0, 100), TraceSeverity.Unexpected,
                                                      "Unexpected exception in GetUserNameByEmail(s) " +
                                                      exception2.StackTrace);
            }

            return(null);
        }
コード例 #4
0
        public override bool RoleExists(string roleName)
        {
            var directoryEntry = LdapManager.Connect(_ldapServer, _ldapPort,
                                                     _ldapUseSsl, _ldapGroupContainer,
                                                     _ldapUserName, _ldapPassword, _ldapSimpleBind);

            var directorySearcher = new DirectorySearcher(directoryEntry)
            {
                Filter = String.Format("(&(ObjectClass=group)({0}={1}))",
                                       _ldapGroupNameAttribute, roleName),
                SearchScope = _ldapUserSearchScope
            };

            var result = directorySearcher.FindAll();

            if (result.Count > 0)
            {
                return(true);
            }

            return(false);
        }
コード例 #5
0
        public static DirectoryEntry ProviderNode(string providerName, bool IsProviderMembership, out string _server, out int _port, out bool _useSSL, out string _path,
                                                  out string _username, out string _password, out string _userNameAttribute, out SearchScope _scope, out bool _simpleBind)
        {
            XmlNode provider = new XmlDocument();
            var     ldapPath = string.Empty;

            var path        = SPUtility.GetVersionedGenericSetupPath(@"WebServices\SecurityToken\web.config", 15);
            var xmlDocument = new XmlDocument();

            xmlDocument.Load(path);

            if (IsProviderMembership)
            {
                provider =
                    xmlDocument.SelectSingleNode(
                        (String.Format("configuration/system.web/membership/providers/add[@name='{0}']", providerName)));
            }
            else
            {
                provider =
                    xmlDocument.SelectSingleNode(
                        (String.Format("configuration/system.web/roleManager/providers/add[@name='{0}']", providerName)));
            }

            try
            {
                _server = (provider.Attributes["server"].Value == null)
                                  ? "localhost"
                                  : provider.Attributes["server"].Value;
            }
            catch (NullReferenceException)
            {
                _server = "localhost";
            }

            try
            {
                _port = (provider.Attributes["port"].Value == null) ? 389 : Convert.ToInt32(provider.Attributes["port"].Value);
            }
            catch (NullReferenceException)
            {
                _port = 389;
            }

            try
            {
                _useSSL = (provider.Attributes["useSSL"].Value != null) && Convert.ToBoolean(provider.Attributes["useSSL"].Value);
            }
            catch (NullReferenceException)
            {
                _useSSL = false;
            }


            _path = string.Empty;

            try
            {
                _username = provider.Attributes["Username"].Value ?? "";
            }
            catch (NullReferenceException)
            {
                //Attribute not present
                _username = string.Empty;
            }

            try
            {
                _password = provider.Attributes["Password"].Value ?? "";
            }
            catch (NullReferenceException)
            {
                //Attribute not present
                _password = string.Empty;
            }

            try
            {
                _simpleBind = (provider.Attributes["simpleBind"].Value != null) && Convert.ToBoolean(provider.Attributes["simpleBind"].Value);
            }
            catch (NullReferenceException)
            {
                _simpleBind = false;
            }

            if (IsProviderMembership)
            {
                try
                {
                    _path = provider.Attributes["userContainer"].Value ?? string.Empty;
                }
                catch (NullReferenceException)
                {
                    _path = string.Empty;
                }
            }
            else
            {
                try
                {
                    _path = provider.Attributes["groupContainer"].Value ?? string.Empty;
                }
                catch (NullReferenceException)
                {
                    _path = string.Empty;
                }
            }

            try
            {
                _userNameAttribute = provider.Attributes["userNameAttribute"].Value ?? "userPrincipalName";
            }
            catch (NullReferenceException)
            {
                _userNameAttribute = "userPrincipalName";
            }

            try
            {
                var scope = (provider.Attributes["scope"].Value.ToUpper() == null) ? "SUBTREE" :
                            provider.Attributes["scope"].Value;

                switch (scope)
                {
                case "BASE":
                    _scope = SearchScope.Base;
                    break;

                case "ONELEVEL":
                    _scope = SearchScope.OneLevel;
                    break;

                case "SUBTREE":
                    _scope = SearchScope.Subtree;
                    break;

                default:
                    _scope = SearchScope.Subtree;
                    break;
                }
            }
            catch (NullReferenceException)
            {
                _scope = SearchScope.Subtree;
            }


            var directoryEntry = LdapManager.Connect(_server, _port, _useSSL, _path, _username, _password, _simpleBind);

            return(directoryEntry);
        }
コード例 #6
0
        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            var users = new MembershipUserCollection();

            totalRecords = 0;

            try
            {
                var directoryEntry = LdapManager.Connect(_ldapServer, _ldapPort,
                                                         _ldapUseSsl,
                                                         _ldapUserContainer,
                                                         _ldapUserName, _ldapPassword,
                                                         _ldapSimpleBind);

                directoryEntry.Username = _ldapUserName;
                directoryEntry.Password = _ldapPassword;

                var directorySearcher = new DirectorySearcher(directoryEntry)
                {
                    Filter =
                        String.Format(
                            "(&(ObjectClass={0})(mail=*{1}*))",
                            _ldapUserObjectClass,
                            emailToMatch),
                    SearchScope =
                        _ldapUserSearchScope,
                    PageSize = pageSize
                };
                var results = directorySearcher.FindAll();
                totalRecords = results.Count;

                {
                    totalRecords = results.Count;

                    if (pageSize == Int32.MaxValue)
                    {
                        pageSize = totalRecords;
                    }

                    var i = pageIndex * pageSize;

                    for (var n = i; (n < (i + pageSize)) && (n < totalRecords); n++)
                    {
                        users.Add(GetUserFromSearchResult(results[n].GetDirectoryEntry()));
                    }
                }
            }
            catch (ActiveDirectoryServerDownException exception)
            {
                SPDiagnosticsService.Local.WriteTrace(100,
                                                      new SPDiagnosticsCategory("NaupliusADLDSProvider",
                                                                                TraceSeverity.High, EventSeverity.Error,
                                                                                0, 100), TraceSeverity.High,
                                                      "AD LDS Server is not responding " +
                                                      exception.StackTrace);
            }
            catch (Exception exception2)
            {
                SPDiagnosticsService.Local.WriteTrace(100,
                                                      new SPDiagnosticsCategory("NaupliusADLDSProvider",
                                                                                TraceSeverity.High, EventSeverity.Error,
                                                                                0, 100), TraceSeverity.Unexpected,
                                                      "Unexpected exception in FindUsersByEmail(sii) " +
                                                      exception2.StackTrace);
            }

            return(users);
        }