Hi,
Is there a way to disable ASP.Net membership provider from using a web.config?
Currently it stores a connection string and a sqlMembershipProvider in a web.config which is not where i'd like it to be.
Thank you
Hi,
Is there a way to disable ASP.Net membership provider from using a web.config?
Currently it stores a connection string and a sqlMembershipProvider in a web.config which is not where i'd like it to be.
Thank you
The provider plumbing is configuration driven.
Depending on your level of experience, you might be able to roll your own providers that are initialized other ways but it is definitely not a trivial task and is fraught with subtle pitfalls that will surface at the most inopportune moments, perhaps when your boss logs in and his network password is leaked.
Attempting to initialize the intrinsic providers programmatically will be an exercise in frustration.
It might help if you were to explain why you don't like using a configuration file for, umm, configuration?
You can typically use a provider just after calling its default constructor and call Initialize
on it. However, it is impossible to use the System.Web.Security.Membership
abstraction, without the web.config, because the Membership
class is hard-wired to the configuration file. When using the Membership
class, it will instantiate types that you configured in the configuration file.
As I said, when you don't want to configure it in the configuration file, you can create it in code. This would be a nice approach, especially when you have your own IoC framework (you could see the Membership
facade as an IoC implementation just for types deriving from MembershipProvider
). Here is an example of how for create a SqlMembershipProvider
:
var configuration = new NameValueCollection();
configuration.Add("name", "SqlProvider");
configuration.Add("connectionStringName", "SqlServices");
configuration.Add("applicationName", "MyApplication");
configuration.Add("enablePasswordRetrieval", "false");
configuration.Add("enablePasswordReset", "true");
configuration.Add("requiresQuestionAndAnswer", "true");
configuration.Add("requiresUniqueEmail", "false");
configuration.Add("passwordFormat", "Hashed");
configuration.Add("maxInvalidPasswordAttempts", "5");
configuration.Add("passwordAttemptWindow=", "10");
var provider = new SqlMembershipProvider();
provider.Initialize("SqlProvider", configuration);
// And here store it in a static field or register it with your
// favorite IoC container.
container.RegisterSingle<MembershipProvider>(provider);
Good luck.