Exemple #1
0
        public override string[] GetRolesForUser(string username)
        {
            var userRoles          = new List <string>();
            var _server            = string.Empty;
            var _port              = 389;
            var _useSSL            = false;
            var _path              = string.Empty;
            var _username          = string.Empty;
            var _password          = string.Empty;
            var _userNameAttribute = string.Empty;
            var _scope             = new SearchScope();
            var _simpleBind        = false;

            var directoryEntry = StsManager.ProviderNode(Name, false, out _server, out _port, out _useSSL,
                                                         out _path, out _username, out _password, out _userNameAttribute, out _scope, out _simpleBind);

            var directorySearcher = new DirectorySearcher(directoryEntry)
            {
                Filter = String.Format("(&(ObjectClass=user)({0}={1}))",
                                       _userNameAttribute,
                                       username),
                SearchScope = _scope
            };

            var results = directorySearcher.FindAll();

            foreach (SearchResult result in results)
            {
                var roleName = result.GetDirectoryEntry();
                userRoles.Add(roleName.Properties["distinguishedName"].Value.ToString());
            }

            return(userRoles.ToArray());
        }
Exemple #2
0
        public override bool ValidateUser(string username, string password)
        {
            bool isValid = false;

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return(isValid);
            }

            string _server;
            var    _port              = 389;
            var    _useSSL            = false;
            var    _path              = string.Empty;
            var    _username          = string.Empty;
            var    _password          = string.Empty;
            var    _userNameAttribute = string.Empty;
            var    _scope             = new SearchScope();
            var    _simpleBind        = false;

            var directoryEntry = StsManager.ProviderNode(Name, true, out _server, out _port, out _useSSL, out _path, out _username,
                                                         out _password, out _userNameAttribute, out _scope, out _simpleBind);

            var credential          = new NetworkCredential(username, password);
            var directoryIdentifier = new LdapDirectoryIdentifier(_server, Convert.ToInt32(_port));
            var connection          = new LdapConnection(directoryIdentifier, credential, AuthType.Basic);

            if (_useSSL)
            {
                connection.SessionOptions.SecureSocketLayer = true;
            }
            else
            {
                connection.SessionOptions.Signing = true;
                connection.SessionOptions.Sealing = true;
            }

            try
            {
                connection.Bind(credential);
                isValid = true;
            }
            catch (ActiveDirectoryServerDownException exception)
            {
                isValid = false;
                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)
            {
                //No result code mapping available
                isValid = false;
                SPDiagnosticsService.Local.WriteTrace(100,
                                                      new SPDiagnosticsCategory("NaupliusADLDSProvider",
                                                                                TraceSeverity.High, EventSeverity.Error,
                                                                                0, 100), TraceSeverity.Unexpected,
                                                      "Unexpected exception in ValidateUser(ss) " +
                                                      exception2.StackTrace);
            }

            return(isValid);
        }