I'm going to refer to the MVC startup example that comes with Visual Studio 2008 for simplicity. Its the sample application that has the Account and Home controllors using the AccountModels.cs.
I would like to modify the application so that on the user login screen, the user must enter a User name, Password and Selection from a drop down box. This selection option will determine which connectionString the application will use for Membership and
Roles.
My web.config file would look something like this:
I would also like to have it so if the user clicks on "remember me" at login, it will also remember their role information so that my controllers could still user the [Authorize] filters.
I just realized that my web.config examples above are from two separate projects. One is using a SQL database, the other a MySQL. Please ignore the code details, as my question is more conceptual.
I would like to modify the application so that on the user login screen, the user must enter a User name, Password and Selection from a drop down box.
You should modify the model posting to take care of this. Then you should pass the connectionstring to membership provider. Please look into default MVC code to see how it initializes the Membership.
Are you talking about making a custom MembershipProvider? (I was thinking about this and overriding the validation function, but I dont know how it needs to be implemented).
This is a messy hack job but I ended up making this work by using the FormsService, as provided by the default MVC application. I stored both the username and the Selection Option into the FormsService. This kept it simple for persistence login (if selected
by the user).
I have a custom repository interface that initializes all my repositories used in the controls. In this interface, I accessed the HttpContext.Current.User.Identity which held the username + selection option. With a quick substring I was able to determine
the selection option and use that to lookup the connectionString in the web.config.
Again, probably not the cleanest way of doing it but as of now everything works. Please let me know if this could be improved upon.
Thanks!
Marked as answer by solidfish on Mar 20, 2012 09:32 PM
solidfish
Member
37 Points
32 Posts
Supporting multiple connectionStrings for Memberships and Roles based on User Login
Mar 01, 2012 11:12 PM|LINK
I'm going to refer to the MVC startup example that comes with Visual Studio 2008 for simplicity. Its the sample application that has the Account and Home controllors using the AccountModels.cs.
I would like to modify the application so that on the user login screen, the user must enter a User name, Password and Selection from a drop down box. This selection option will determine which connectionString the application will use for Membership and Roles.
My web.config file would look something like this:
<connectionStrings> <add name="optionA" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdbA.mdf;User Instance=true" providerName="System.Data.SqlClient"/> <add name="optionB" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdbB.mdf;User Instance=true" providerName="System.Data.SqlClient"/> <add name="optionC" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdbC.mdf;User Instance=true" providerName="System.Data.SqlClient"/> <add name="optionD" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdbD.mdf;User Instance=true" providerName="System.Data.SqlClient"/> </connectionStrings>And my RoleProvider would look something like this:
<roleManager enabled="true" defaultProvider="optA" cacheRolesInCookie="true" createPersistentCookie="true" cookieProtection="All"> <providers> <clear /> <add name="optA" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.4.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="optionA" applicationName="test" /> <add name="optB" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.4.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="optionB" applicationName="test" /> <add name="optC" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.4.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="optionC" applicationName="test" /> <add name="optD" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.4.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="optionD" applicationName="test" /> </providers> </roleManager>I would also like to have it so if the user clicks on "remember me" at login, it will also remember their role information so that my controllers could still user the [Authorize] filters.
Thank you for your help!
solidfish
Member
37 Points
32 Posts
Re: Supporting multiple connectionStrings for Memberships and Roles based on User Login
Mar 01, 2012 11:15 PM|LINK
I just realized that my web.config examples above are from two separate projects. One is using a SQL database, the other a MySQL. Please ignore the code details, as my question is more conceptual.
ignatandrei
All-Star
134867 Points
21616 Posts
Moderator
MVP
Re: Supporting multiple connectionStrings for Memberships and Roles based on User Login
Mar 02, 2012 04:33 AM|LINK
You should modify the model posting to take care of this. Then you should pass the connectionstring to membership provider. Please look into default MVC code to see how it initializes the Membership.
solidfish
Member
37 Points
32 Posts
Re: Supporting multiple connectionStrings for Memberships and Roles based on User Login
Mar 03, 2012 02:57 PM|LINK
Are you talking about making a custom MembershipProvider? (I was thinking about this and overriding the validation function, but I dont know how it needs to be implemented).
ignatandrei
All-Star
134867 Points
21616 Posts
Moderator
MVP
Re: Supporting multiple connectionStrings for Memberships and Roles based on User Login
Mar 03, 2012 03:53 PM|LINK
solidfish
Member
37 Points
32 Posts
Re: Supporting multiple connectionStrings for Memberships and Roles based on User Login
Mar 20, 2012 09:32 PM|LINK
This is a messy hack job but I ended up making this work by using the FormsService, as provided by the default MVC application. I stored both the username and the Selection Option into the FormsService. This kept it simple for persistence login (if selected by the user).
I have a custom repository interface that initializes all my repositories used in the controls. In this interface, I accessed the HttpContext.Current.User.Identity which held the username + selection option. With a quick substring I was able to determine the selection option and use that to lookup the connectionString in the web.config.
Again, probably not the cleanest way of doing it but as of now everything works. Please let me know if this could be improved upon.
Thanks!