简体   繁体   中英

Programmatically Lookup a User in SharePoint

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM