ProfileProvider: retrieve list of all profiles

2019-05-22 22:43发布

问题:

I'm developing an asp.net intranet website with a ActiveDirectoryMembershipProvider and a SqlProfileProvider.

One of the requirements of my website is to have a "Birthdays" page, which would require me to list all profiles and retrieving the birthday information from it.

I approached the problem as follows:

  • Invoke the Membership.GetAllUsers() static method;
  • Iterate through the list of users and retrieve the profile from the member's user name

This approach, however, failed for the following reasons:

  • The webapp is impersonating the current logged user to retrieve its AD details (identity impersonate="true" in the web.config), so I get an exception "access is denied" when trying to invoke the GetAllUsers
  • If I do try to make the webapp impersonate a super user account then AD returns the user names as username@domain-name format, but in my profile provider they were initially stored as domain-name\username format.

So, how would you go around this problem to retrieve the whole list of profiles for any member of the organization?

回答1:

Although I've never done it before, you could attempt to create a secondary impersonation context, that when established, the call to GetAllUsers should succeed.

Have a look at http://chiragrdarji.blogspot.com/2007/03/impersonation-using-code.html, this chap appears to have achieved a change in security context by using System.Security.Principal.WindowsIdentity class along with System.Security.Principal.WindowsImpersonationContext. Might be worth checking out.



回答2:

There is a ProfileManager with the a method GetAllProfiles():

http://msdn.microsoft.com/en-us/library/system.web.profile.profilemanager.getallprofiles.aspx



回答3:

ProfileInfoCollection profiles = ProfileManager.GetAllProfiles(ProfileAuthenticationOption.All);
foreach (ProfileInfo pi in profiles)
{
    ProfileCommon p = Profile.GetProfile(pi.UserName);
    countries.Add(p.Country);        
}