C# and MySQL

O

Ook

Has anyone sucessfully connected a C# asp form to a MySQL database? If so,
would you care to share how you did it?
 
N

Nicholas Paldino [.NET/C# MVP]

Ook,

If your ASP page is running on a different machine than the MySQL
database, then by default, you won't be able to connect. The reason for
this is that ASP.NET pages by defualt run under the local ASPNET user
account, which has limited rights. It doesn't have the right to open
network connections.

What you need to do is impersonate a user that has rights to the
database either through modification of the web.config file in the directory
the page is located, or by calling the Impersonate method on the
WindowsIndentity class.

Hope this helps.
 
O

Ook

OK, now I'm confused :)

This application will be used by 50 users each sitting at their own
workstation. The MySQL database is sitting on yet another machine. Is this
not something that is easily done with c#?


Nicholas Paldino said:
Ook,

If your ASP page is running on a different machine than the MySQL
database, then by default, you won't be able to connect. The reason for
this is that ASP.NET pages by defualt run under the local ASPNET user
account, which has limited rights. It doesn't have the right to open
network connections.

What you need to do is impersonate a user that has rights to the
database either through modification of the web.config file in the directory
the page is located, or by calling the Impersonate method on the
WindowsIndentity class.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ook said:
Has anyone sucessfully connected a C# asp form to a MySQL database? If so,
would you care to share how you did it?
 
N

Nicholas Paldino [.NET/C# MVP]

Ook,

The application will be accessed through a browser of some sort, and the
code that actually does the accessing of the database is going to reside on
a server that the users connect to. You have to make sure that the process
that it runs under (the code accessing the database) has the appropriate
access rights. By default, ASP.NET does not (you mentioned an ASP form).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ook said:
OK, now I'm confused :)

This application will be used by 50 users each sitting at their own
workstation. The MySQL database is sitting on yet another machine. Is this
not something that is easily done with c#?


message news:[email protected]...
Ook,

If your ASP page is running on a different machine than the MySQL
database, then by default, you won't be able to connect. The reason for
this is that ASP.NET pages by defualt run under the local ASPNET user
account, which has limited rights. It doesn't have the right to open
network connections.

What you need to do is impersonate a user that has rights to the
database either through modification of the web.config file in the directory
the page is located, or by calling the Impersonate method on the
WindowsIndentity class.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ook said:
Has anyone sucessfully connected a C# asp form to a MySQL database? If so,
would you care to share how you did it?
 
S

Sami Vaaraniemi

Comments inline:

Nicholas Paldino said:
Ook,

If your ASP page is running on a different machine than the MySQL
database, then by default, you won't be able to connect. The reason for
this is that ASP.NET pages by defualt run under the local ASPNET user
account, which has limited rights. It doesn't have the right to open
network connections.

Huh? Are you sure about this? I can't test this right now but I doubt this
is the case.
What you need to do is impersonate a user that has rights to the
database either through modification of the web.config file in the directory
the page is located, or by calling the Impersonate method on the
WindowsIndentity class.

I've written an ASP.NET web app that connects to an MySQL database using the
ByteFX driver (http://www.bytefx.com/). The programming model is practically
identical to that of SqlClient, just the class names are different. There is
no need for impersonation, you just set up the database connection string as
usual.
Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ook said:
Has anyone sucessfully connected a C# asp form to a MySQL database? If so,
would you care to share how you did it?
 
N

Nicholas Paldino [.NET/C# MVP]

Sami,

If your server is on the same machine as IIS is, then you will not have
a problem with this. However, if the MySql server is not the IIS machine,
then you will have to establish a network connection. The default settings
of an ASP.NET page prevent this from ocurring, and this is a common issue
with people using ASP.NET that are unaware of the security settings in it.

To get around it, you have to do what I said (configure impersonation in
web.config, or make a call in the page itself) so that you obtain rights to
access another machine in the network.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Sami Vaaraniemi said:
Comments inline:

message news:[email protected]...
Ook,

If your ASP page is running on a different machine than the MySQL
database, then by default, you won't be able to connect. The reason for
this is that ASP.NET pages by defualt run under the local ASPNET user
account, which has limited rights. It doesn't have the right to open
network connections.

Huh? Are you sure about this? I can't test this right now but I doubt this
is the case.
What you need to do is impersonate a user that has rights to the
database either through modification of the web.config file in the directory
the page is located, or by calling the Impersonate method on the
WindowsIndentity class.

I've written an ASP.NET web app that connects to an MySQL database using the
ByteFX driver (http://www.bytefx.com/). The programming model is practically
identical to that of SqlClient, just the class names are different. There is
no need for impersonation, you just set up the database connection string as
usual.


Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ook said:
Has anyone sucessfully connected a C# asp form to a MySQL database? If so,
would you care to share how you did it?
 
S

Sami Vaaraniemi

Nicholas Paldino said:
Sami,

If your server is on the same machine as IIS is, then you will not have
a problem with this. However, if the MySql server is not the IIS machine,
then you will have to establish a network connection. The default settings
of an ASP.NET page prevent this from ocurring, and this is a common issue
with people using ASP.NET that are unaware of the security settings in it.

Odd. My machine.config specifies userName="machine" (so its the
low-privilege account), and my app's web.config specifies no impersonation.
Further, if my web app tries to e.g., write to the event log, it fails with
a SecurityException as expected. Yet my web application is successfully
calling the google web service, which does establish a network connection.
If what you are saying is true, then the web service call should fail too
shouldn't it?

You're right about my database, it's local. But if my memory serves me in
the previous project I worked on I was calling a remote SQL database from a
low-privilege ASPNET account with no impersonation. I guess I'm just going
to have to try this out.

Sami
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top