Solved

Asp.net on the fly connection string changes

Posted on 2007-04-03
9
709 Views
Last Modified: 2013-11-26
I am building a VS2005 ASP.net C# application that requires database access
with on the fly connection string changes based on user logged in.

All that needs to change is the database name / server name
using a trusted connection and no user password is stored in the string.

This is what I was thinking of doing.. or do you have a better
solution?

Create a class that uses the connection string builder class
look up the user name and the associated db server/dbname stored
in a database  table and return the connection string.
On each page that requires access to the db use the class to set the connection string .

Thanks
0
Comment
Question by:mustang_larry
  • 3
  • 2
9 Comments
 
LVL 1

Expert Comment

by:cmwhite
ID: 18845954
I personally in the past have done roughly that, except had my Connection String roughly prebuilt so no reason for the connection builder. IE:

string u = Request("name");

string cs = "Driver={SQL Server};Server=myServerAddress;Database=" + u + "Trusted_Connection=Yes";

SqlConnection sqlconn;

etc..

That would work for your case, but mine was a bit different I did require a specifc password to verify against so I did the same thing though except passed it encrypted.

Hope this helps

Chris
0
 
LVL 21

Expert Comment

by:Yurich
ID: 18846385
a little flaw in the logic... you need a connection string to connect to a database to look up in the table for a connection string... unless of course you're going to have some kind of system connection string and then customizable customer/user connection string...

Depending on the number of databases it would probably make sense to keep it in web.config file or if it's too big, probably in some separate file (possible xml file as it can be structured in proper data-storage way). having a separate "system" connection is acceptable also, but little bit strange.

Having a separate class for this seems to me a bit overdoing. you can initialize your connection string once when you user logs in, and then you either can pass your connection string along the pages (using session variable) or initialize it on each page.

good luck,
yurich
0
 

Author Comment

by:mustang_larry
ID: 18846553
Yurich thanks for your ideas

My thought was in the web.config have one connection string to the "master" users
database which the class would use.

The main goal is to have one set of aspx pages for all users.

The number of connection strings would be 20-50 for now but could grow.

I am using the Membership Classes was thinking that adding the server and dbnames
linked to  the users id. I want to keep things clean instead of maintaining separate xml file or
web.conf entries since they would really need to be synced when users
are added or removed.

The session variable sounds like a good idea this way it won't have to
make multiple db requests for each page load.. As long as this isn't
a security risk.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 21

Accepted Solution

by:
Yurich earned 125 total points
ID: 18847208
The session variable is quite secure as it's server-based (it does consume some server resources, but it's reasonable in your situation).

I didn't realize it would be 20-50 variations, I thought of 3-4, in whcih case it does make sense to keep your master connection settings in web.config and everything else in a master database.

Cheers,
Yurich
0
 

Author Comment

by:mustang_larry
ID: 18853890
Anybody else done this? and pros cons other possible methods?
0
 
LVL 21

Expert Comment

by:Yurich
ID: 19250700
I'm still interested.

Regards,
Yurich
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now