I'm in the process of moving code out of App_Code
into a class library.
I create users programmatically using Membership.CreateUser
.
How can I continue to do this inside my class library where there's no access to the membership provider I have configured in web.config
?
The ASP.NET membership provider is a feature that enables ASP.NET developers to create Web sites that allow users to create unique user name and password combinations. With this facility, any user can establish an account with the site, and sign in for exclusive access to the site and its services.
The Membership class is used in ASP.NET applications to validate user credentials and manage user settings such as passwords and email addresses. The Membership class can be used on its own, or in conjunction with the FormsAuthentication to create a complete system for authenticating users of a Web application or site.
SQLMembershipProvider : It is used to store user information in a SQL Server database. ActiveDirectoryMembershipProvider : It is used to store user information in an Active Directory.
Before I show you how the code part works - you need to understand how the settings get loaded up in this kind of scenario.
When you have a Web Application which is going to load your .dll
file - and that .dll
file is going to access the MembershipProvider
which the application has configured - you have to make a few assumptions.
MembershipProvider
MembershipProvider
in its Web.Config
.dll
correctlyBecause your .dll
file should be included in the Web Application's /bin
directory, you should be able to rely on the Web Application's configuration rather than having to supply your own.
To do this, start with what Oded mentions in his answer - create a reference to System.Web.Security
within your .dll
's code - then within that file you can do something like the following:
if (Membership.Provider != null) {
Membership.Provider.CreateUser( ... );
} else {
// Do something appropriate in a case where there is no Membership Provider
}
At this point - if the above doesn't work, it is likely because your Web Application doesn't have an appropriate provider configured.
A note on why to do it this way...
The reason you should let the Web Application provide the configuration is to comply with the principle of Separation of Concerns. The MembershipProvider
is an abstract class which provides default functionality - with very little implementation.
In other words - it defines that to manage members, you need to be able to perform operations such as CreateUser()
and GetAllUsers()
. It also says that you should be able to configure settings, such as specifying a PasswordFormat
and determining whether or not each user RequiresUniqueEmail
.
What it doesn't do is tell you where to store your User information. It leaves that up to the implementor (System.Web.Providers.DefaultMembershipProvider
or YourNS.YourMembershipProvider
).
The Application which uses the MembershipProvider
then determines what settings to provide and which implementation to use. In other words - it's the job of YourNS.YourMembershipProvider
to specify how information is managed, but it is likely the Application who should determine what ConnectionString
to use in its storage, etc.
So - following the pattern I outlined above allows you to provide three separate layers:
MembershipProvider
,MembershipProvider
implementation, andMembershipProvider
is configured - and does something with it on behalf of the application (*this is the layer you're describing in your post, I believe)Notice that if you follow this pattern - you can now switch MembershipProviders
at a later time without having to alter either of the other layers - because those layers rely on the base class of MembershipProvider
- rather than relying on your specific implementation. This can be hugely valuable.
I solved my issue by copying the whole web.config to my app.config. I was using a console application. I thought I had copied the necessary sections but until I copied everything, the data was not getting to database
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With