I looked at membership provider documentation and there should be two GetAllUsers method in membership provider (http://msdn.microsoft.com/en-us/library/system.web.security.membership.getallusers ).
But when I look at methods exposed by System.Web.Security (in
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.ApplicationServices.dll )
It only has one method (the one that has paging capability).
Where can I find a method to return all users from database? It seems there is no way to get list of all users or find how many user is in the database.
--Update
looking at System.Web.Security, i found that SqlMembershipProvider is defined as follow:
public class SqlMembershipProvider : System.Web.Security.MembershipProvider
but this class doesn't have any public GetAllUsers() method.
How can I access its base GetAllUsers method?
3 Answers
Answers 1
I think you should be looking in the System.Web.dll not System.Web.ApplicationServices.dll
Answers 2
The way it works is that you have to have your membership provider, like a SqlMembershipProvider, configured in your web.config
. The Membership.GetAllUsers method will then take the default configured MembershipProvider
and call its method GetAllUsers and pass in 0
for the current page index and int.MaxValue
for the total records per page (as well as an out int
to get the total records in the store).
If you are having issues calling this method check the following:
- You have the correct .net assemblies referenced
System.Web
System.Web.ApplicationServices
System.Configuration
- You have the correct namespace referenced
System.Web.Security
either- In your
using
statements at the top of the file - Directly on the type where referenced
- In your
- Your
web.config
has a registered defaultMembershipProvider
Notes
- SqlMembershipProvider, which derives from
MembershipProvider
, is not the same type or type hierarchy as typeMembership
. So not all the methods you see onMembership
can be found on types deriving fromMembershipProvider
. - The type
Membership
is static and so all its members including Membership.GetAllUsers are static as well. - The type
Membership
is there as a convenience, most of its members call through to the registered defaultMembershipProvider
.
Code sample:
using System.Web.Security; namespace MyNameSpace { public class MembershipTests { public void Test() { var users = Membership.GetAllUsers(); // same as var totalRecords = 0; users = Membership.GetAllUsers(0, int.MaxValue, out totalRecords); // same as (Membership.Provider gets the default registered MembershipProvider) users = Membership.Provider.GetAllUsers(0, int.MaxValue, out totalRecords); } } }
From the documentation
Remarks
GetAllUsers returns the information for all membership users for an application as a collection of MembershipUser objects. Be careful when using the GetAllUsers method with very large user databases, as the resulting MembershipUserCollection in your ASP.NET page may degrade the performance of your application.
Answers 3
The question is "Where is GetAllUsers in membership provider?"
The answer is that it is not in MembershipProvider. It is in System.Web.Security.Membership
The MSDN link you provided in your question says the same thing.
The method GetAllUsers static and overloaded.
int userCount = 0; MembershipUserCollection muc1 = System.Web.Security.Membership.GetAllUsers(); MembershipUserCollection muc2 = System.Web.Security.Membership.GetAllUsers(0, int.MaxValue, out userCount);
If you are having namespace issues, they shuffled some things around between .net 3.5 and .net 4.0
This bit me several years ago.
.Net 4.0 System.Web.Security.MembershipProvider ambiguous reference?