views:

44

answers:

1

I’ve encountered a situation where I need to be able to lookup a user by their active directory name. I’ve tried looking for the user in the SPSite.Users, SPSite.AllUsers and SPSite.SiteUsers properties, but the user isn’t guaranteed to be in any of those lists. For users who gain permission to the site via an active directory group that has been granted permission to the site, there is no listing for the user in the various user lists, only a record for the active directory group. Is there a way to find a user programmatically in SharePoint without directly having to query active directory via LDAP?

I would ideally like to find a mechanism that works for both forms based authentication and AD authentication, but I need to find a mechanism that works with AD authentication.

+1  A: 

Try SPWeb.EnsureUser:

Checks whether the specified login name belongs to a valid user of the Web site, and if the login name does not already exist, adds it to the Web site

This method works with both AD and FBA. If you are using both, you will need to prefix the username from a non-default provider with something like this:

SPUser webUser = web.EnsureUser("SecondaryProvider:" + userName);
Rich Bennema
That seems backwards that there isn't a global listing you can query, but by after running EnsureUser, the user then shows up in the SPSite.SiteUsers list. You can then grab the SPUser object from the list which is exactly what I need.
ICodeForCoffee