コード例 #1
0
ファイル: Plugin.cs プロジェクト: zhiqian1027/pgina
        /// <summary>
        /// Connects to LDAP Server according to user's credentials.
        /// (These credentials have been stored in the SessionProperties object
        /// during the Gateway stage.)
        /// Retrieves the name of the script file on the user's LDAP account.
        /// </summary>
        private void LdapPart(SessionChangeDescription changeDescription, SessionProperties properties)
        {
            // initializes and sets up a new Ldap connection
            LdapInitialization(properties);
            // Get the LdapServer object from the session properties (created in LdapInitialization)
            LdapServer server = properties.GetTrackedSingle <LdapServer>();

            if (server == null)
            {
                pluginImpl_logger.ErrorFormat("Internal error: LdapServer object not available.");
                return;
            }

            try
            {
                pluginImpl_logger.DebugFormat("AuthenticateUser({0})", properties.Id.ToString());

                // retrieving user's information stored during Gateway stage
                Shared.Types.UserInformation userInfo = properties.GetTrackedSingle <Shared.Types.UserInformation>();
                string userLogin    = properties.GetTracked <string>("UserLogin");
                string userPassword = properties.GetTracked <string>("UserPassword");
                pluginImpl_logger.DebugFormat("Received username: {0}", userLogin);

                // Authenticate the login
                pluginImpl_logger.DebugFormat("Attempting authentication for {0}", userLogin);
                BooleanResult authenticateBool = server.Authenticate(userLogin, userPassword);

                if (!authenticateBool.Success) // authentication and attribute value retrieving didn't work
                {
                    pluginImpl_logger.ErrorFormat("LDAP Authentication failed. {0}", authenticateBool.Message);
                    return;
                }

                // retrieves the script name from Ldap
                this.scriptName = server.GetScriptName();
                pluginImpl_logger.DebugFormat("Name of the script file:  {0}", this.scriptName);

                // cleans up any resources held by the plugin
                LdapEnd(properties);
            }
            catch (Exception e)
            {
                if (e is LdapException)
                {
                    LdapException ldapEx = (e as LdapException);

                    if (ldapEx.ErrorCode == 81)
                    {
                        // Server can't be contacted, set server object to null
                        pluginImpl_logger.ErrorFormat("Server unavailable: {0}, {1}", ldapEx.ServerErrorMessage, e.Message);
                        server.Close();
                        properties.AddTrackedSingle <LdapServer>(null);
                        return;
                    }
                }

                // This is an unexpected error, so set LdapServer object to null, because
                // subsequent stages shouldn't use it, and this indicates to later stages
                // that this stage failed unexpectedly.
                server.Close();
                properties.AddTrackedSingle <LdapServer>(null);
                pluginImpl_logger.ErrorFormat("Exception in LDAP authentication: {0}", e);
                throw;  // Allow pGina service to catch and handle exception
            }
        }
コード例 #2
0
ファイル: Plugin.cs プロジェクト: rafu1/pgina
 /// <summary>
 /// ldap initialization and set-up
 /// </summary>
 public void LdapInitialization(SessionProperties props)
 {
     pluginImpl_logger.Debug("LDAP server initialization and set-up.");
     try
     {
         LdapServer serv = new LdapServer();
         props.AddTrackedSingle<LdapServer>(serv);
     }
     catch (Exception e)
     {
         pluginImpl_logger.ErrorFormat("Failed to create LdapServer: {0}", e);
         props.AddTrackedSingle<LdapServer>(null);
     }
 }