Automatically Add LDAP users on first login and settings

If you have questions about using mojoPortal, you can post them here.

You may want to first review our site administration documentation to see if your question is answered there.

This thread is closed to new posts. You must sign in to post in the forums.
2/2/2011 12:45:21 PM
Gravatar
Total Posts 76

Automatically Add LDAP users on first login and settings

My development of my new intranet is coming together. I have picked and installed MojoPortal as the base.  I have LDAP setup and running.  I have an idea for an add-on (reports pull from our ERP system), and I so far I only have "Hello World" and a table being pulled.   I have lots of questions that I am trying to use the documentation on.  Of course I am not going to post any questions until I go though the documentation at least 20 times.  This post is about LDAP and "Automatically Add LDAP users on first login".  I can't find specific information on this in the forums (or they are over 5 years old).  </Background story>

When a new user logins, the name only shows their login name and not the information from Active Directory.  Also, it does not display the email.  Where is the code for a new user so that I can make sure the information is in the correct spots in the Active Directory or change where it pulls from.  Also, I would like the ability to allow the end user to change their display name.  With LDAP setting on, the box is disabled, but I turned it on before switching to LDAP and it still allows users to change their display name (which I want), but I would like there to be a check in the box. 

Thank you for any reply.  I hope that all makes sense.

2/2/2011 1:12:42 PM
Gravatar
Total Posts 1203
Proud member of the mojoPortal team

Help support mojoPortal!
Add-on modules

Re: Automatically Add LDAP users on first login and settings

Hi Jonathan, if you look at item 8 on the Using OpenLDAP document, you'll see that mojoPortal will attempt to use LDAP attributes "givenName" for first name, and "sn" for last name. I think that this is only being used for OpenLDAP.

If you have the mojoPortal source code, the conversion between LDAP attributes and mojoPortal fields happens in mojoPortal.Business LdapUser.cs. It looks like it's using an LDAP attribute of "mail" to pull the email address if you're using OpenLDAP, or setting it to userName + Domain (from LDAP settings) on ActiveDirectory.

I still think ultimately that it would be helpful to have more of a generic method to map the LDAP attributes to mojoPortal user fields, but I'm not sure if it will be doable.

I do agree that user name should probably be editable within mojoPortal even when using LDAP. That will be a feature request.

Jamie

2/2/2011 1:20:28 PM
Gravatar
Total Posts 1203
Proud member of the mojoPortal team

Help support mojoPortal!
Add-on modules

Re: Automatically Add LDAP users on first login and settings

I should add too that the reason for the discrepancy between the OpenLDAP and AD processing is that AD can be much more restrictive, only allowing a bind of the connection for authentication without returning any attributes for the user. I believe it will try to get the attributes with both types of directories, so you could try setting up attributes for mail, givenName, and sn, and see if that works for you. If you could report your results back here I'd find that very helpful.

Thanks,

Jamie

2/3/2011 9:18:54 AM
Gravatar
Total Posts 76

Re: Automatically Add LDAP users on first login and settings

Thank you for your reply.  I learn more and more every time I dive into the code.  For the active directory, would it be possible to use System.DirectoryServices instead of Novell.Directory.Ldap to pull information about the user?  I am not sure how MojoPortal is authenticating the user, so I when I did my test on System.DirectoryServices I entered my credentials manually in the code below.  It pulls up information about other users (the public information about them.  My login is not a domain admin).  Also in my test, I put everything to a label to see it.  If MojoPortal can gain access to the same entry type, it can not only pull information about the user (name, email), but also pull the groups they are in and if the setting is turned on (one way, two way, etc), synch the groups to the matching internal groups.  (Feature Request I am willing to help with once I understand more about the login procedure). 

Warning: Sloppy code ahead:

using System.DirectoryServices;

public void ad(string user)
        {
            DirectoryEntry Entry = new DirectoryEntry("LDAP://domain.com", "myusername", "password");
            DirectorySearcher Searcher = new DirectorySearcher(Entry);
            System.DirectoryServices.SearchResult result = default(System.DirectoryServices.SearchResult);
            try
            {
                Searcher.Filter = ("(anr=" + user + ")");
                result = Searcher.FindOne();

                // Get the properties for 'mySearchResult'.
                ResultPropertyCollection myResultPropColl = default(ResultPropertyCollection);
                myResultPropColl = result.Properties;
                string myKey = null;
                bool usekey = false;
                foreach (string myKey_loopVariable in myResultPropColl.PropertyNames)
                {
                    myKey = myKey_loopVariable;
                    usekey = true;
                    switch (Strings.LCase(myKey))
                    {
                        case "cn":
                            break;
                        case "mail":
                            break;
                        case "samaccountname":
                            break;
                        case "givenname":
                            break;
                        case "sn":
                            break;
                        default:
                            usekey = false;
                            break;
                    }
                    if (usekey)
                    {
                        object myCollection = null;
                        if (myResultPropColl(myKey).Count > 0)
                        {
                            o.Text += "<br>" + myKey + ": " + myResultPropColl(myKey).Item(0);
                        }
                        else
                        {
                              //handle default here
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string debug = ex.Message;
            }
        }

Bottom line, do you think its possible to include some of this code in the way mojoPortal authenticates users with ldap settings?

2/3/2011 9:44:16 AM
Gravatar
Total Posts 1203
Proud member of the mojoPortal team

Help support mojoPortal!
Add-on modules

Re: Automatically Add LDAP users on first login and settings

Hi Jonathan. Yesterday I was testing with our own AD server and I've come to the same conclusion as you--I think the problem in pulling attributes is the use of the Novell LDAP library against AD.

I have already started working on some code modifications using System.DirectoryServices for the AD authentication, while leaving the working OpenLDAP authentication alone, and I'll submit the changes to Joe for approval and hopefully to be included in the mojoPortal core. Thanks for posting the "sloppy" code. I'd gotten to the point where I can authenticate with System.DirectoryServices, but the properties I was iterating through weren't what I expected. I'll compare my changes with your sample and see if I can find out where I'm off.

I'll let you know how the progress goes!

Jamie

2/3/2011 10:09:42 AM
Gravatar
Total Posts 76

Re: Automatically Add LDAP users on first login and settings

I am glad I was able to contribute something to this project.  From what I can tell from this page (http://msdn.microsoft.com/en-us/library/system.directoryservices.searchresult.properties.aspx), there are a list of "keys" and then each key has its own collection of options.  So even though the name for example would only have one entry, it still has it under a collection.  I have been looking at trying to get this information from AD off and on for about 6 years.  One thing I really like about mojoPortal is I can use a forum login and still use active directory authentication.  I like I don't have to use IIS login, because when you use windows auto login you can't specify the default domain in IIS.  Most of my users (using computers not directly on the ad network) have to put in their login anyway.

This is the code (non sloppy) is the Microsoft code that runs though all of the properties. (Copied from that link)

// Get the properties for 'mySearchResult'.
ResultPropertyCollection myResultPropColl;
myResultPropColl = mySearchResult.Properties;
Console.WriteLine("The properties of the " +
                  "'mySearchResult' are :");
foreach( string myKey in myResultPropColl.PropertyNames)
{
   string tab = "    ";
   Console.WriteLine(myKey + " = ");
   foreach( Object myCollection in myResultPropColl[myKey])
   {
      Console.WriteLine(tab + myCollection);
   }
}

2/7/2011 4:12:04 PM
Gravatar
Total Posts 1203
Proud member of the mojoPortal team

Help support mojoPortal!
Add-on modules

Re: Automatically Add LDAP users on first login and settings

Hi Jonathan, I've completed code changes to use System.DirectoryServices for authentication with ActiveDirectory. it will now reliably pull the name (from "cn") and email (from "mail") fields during LDAP user creation. If it fails to get these fields for any reason, it will fall back to the original method of determining the field contents. These changes are in the 2.3.6.2 update, and you can also get them through TortoiseHg update.

I spoke with Joe about your ideas about synchronizing the mojoPortal roles and AD group memberships. Neither of us are very excited about doing that. The mojoPortal role system needs to be stand-alone for best security, and mixing in your AD groups could inadvertently open an attack vector to your internal network. With that said, you could certainly work on your own project to synchronize your groups and roles, and add it as a custom item to the administration menu. This would allow you to have that functionality without messing up your ability to upgrade mojoPortal cleanly.

Hope that helps!

Jamie

You must sign in to post in the forums. This thread is closed to new posts.