Most of our .net IIS webapps use the System.Web.Security in .net to validate user logins in AD. However, during a company transition period, we currently have 4 AD domain controllers. I am looking for a way to test the AD user login against other AD connection strings in the event that one is down or that a user is using a different domain password.
Our code is just:
bool isValidAdUser = Membership.ValidateUser(model.UserName, model.Password);
And the connection string is set inside the Membership tag in web.config. How I can I add more connection strings?
You can't change the connection string of a
MembershipProviderat runtime.You can however implement a custom
MembershipProviderthat delegates its method execution to the different Active Directory membership providers registered in theweb.configfile, where each of these has its own connection string to the corresponding domain.Consider a custom
MembershipProvider- namedDelegatingMembershipProvider- that implements itsValidateUsercall by looping over each registered Active Directory membership provider until the first one of these returnstrueor all of themfalse.About checking whether the given domain controller is online; you might wrap the
ValidateUsercall with atry/catch, assuming that this call towards an offline domain controller fails as such - you'll have to check that one.Below code shows how such a
MembershipProvidercan look like. Find more details at Implementing a Membership Provider.For brevity, the code just shows the essential parts.
You register this
DelegatingMembershipProviderinweb.configas below. Notice that this custom membership provider needs to be set as the default one.