Prompt user for connection string

  • Thread starter Thread starter McGeeky
  • Start date Start date
M

McGeeky

Hi.

I am using SQLDataSources with the connection string hardcoded in the
web.config file under the connectionStrings element. However, I want users
to be able to enter a username and password on a login page which I want to
subsequently use in the connection string. Is that possible?

Thanks.
 
I am using SQLDataSources with the connection string hardcoded in the
web.config file under the connectionStrings element. However, I want users
to be able to enter a username and password on a login page which I want
to
subsequently use in the connection string. Is that possible?

Yes - store the username and password (encrypted, if possible) in the
Session variable, and then just use the String.Replace() method on the
existing connection string.
 
Yes, you need to provide some gui for entering the credentials, make the
connection string out of the user input and save it in a session variable
where it will be available for all other pages. You may consider using the
Login control.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
Thanks Mark and Eliyahu.

When replacing the existing connection string, will that apply to that
user's session only or is it a global change?
 
When replacing the existing connection string, will that apply to that
user's session only or is it a global change?

It won't be a global change unless you actually write the values back to the
web.config!

Obviously, this would be utter lunacy, as it would keep getting overwritten
every time somebody logged in... :-)

Also, I have a feeling this would cause your app to cycle (not 100% sure
about this), so that really is the very last thing you want to do!

You could store the modified connection string in a Session variable and
then fetch it from there every time you need it, rather then from
web.config...
 
Hi Mark,

That doesn't sound like a great idea! I suppose what I mean is this;
currently when dropping a SqlDataSource control on to my web page I specify
the connection string to use which is defined in the web.config file. Will I
still be able to use this technique when creating a custom connection
string?

Thanks.
 
when dropping a SqlDataSource control on to my web page

Oh right... Sorry, I have no idea - I never go anywhere near those
controls...

However, if you are able to specify a value for it, surely you are able to
specify another value for it, or is it all done at design time...?
 
Hi Mark,

I think I can set the connection string of a SqlDataSource in the Page_Load
method. This should do the trick. Only issue with this is it means some
initialization code at the start of every page. Shame because I liked the
configuration only approach.
 
I think I can set the connection string of a SqlDataSource in the
Page_Load
method. This should do the trick. Only issue with this is it means some
initialization code at the start of every page.

Create a page base class and have all your pages inherit from it...
Shame because I liked the configuration only approach.

Each to their own... I find it immensely restrictive...
 
Sure, it's possible.

Make sure you're protected regarding SQL injection attacks, though.



Juan
====
 
Hi.
I am using SQLDataSources with the connection string hardcoded in the
web.config file under the connectionStrings element. However, I want
users to be able to enter a username and password on a login page
which I want to subsequently use in the connection string. Is that
possible?

Thanks.

One disadvantage of this: as every user has his own connection string, you
don't benefit as much from connection pooling. This means your app will use
a higher number of database connections than really needed.

Hans Kesting
 
Back
Top