473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Troubles Querying Database in Application_Start



I'm having trouble with my security model, Application_Start, and accessing
my database.

My ASP.NET app is only going to be running in an intranet environment (not
on the public Internet). The production environment will have installed
everything locally (i.e., IIS, SQL Server, .NET Framework, etc., all on a
Win2k Server).

In IIS, I have disabled anonymous access so that only Integrated Windows
authentication is used. Then in my Web.config file I've set it up to use
Windows authentication, as such:

<appSettings>

<add key="connectionString"
value="database=RegPerfectDb;server=localhost;Trus ted_Connection=true" />

</appSettings>

<system.web>

<authentication mode="Windows" />

<authorization>

<deny users="?" />

</authorization>

<identity impersonate="true" />

</system.web>

Then, in my Application_Start event in Global.asax, I need to run a query or
two to get some application-wide data from our database and store it in the
application cache.

When I try to access a database from Application_Start, I get the following
error:

System.Data.SqlClient.SqlException: Cannot open database requested in login
'DatabaseName'. Login fails. Login failed for user 'MACHINENAME\ASPNET'.

It's as if it is using the default ASP.NET worker process account (which is
not what I want). I need it to authenticate/authorize the logged in user
(which has access to the database) and use that user to access the database.

So, my questions are: what am I doing wrong? how do I set up IIS,
Web.config, database connection, etc. so that everything is properly
authenticated/authorized to the logged in user so that I can query the
database in Application_Start?

Thanks in advance for your help.



Nov 18 '05 #1
6 2705
This is because when the application starts, it isn't running under any
particular user. That only happens for page requests - which is processed
after the application is started. Application_Start runs only once - and so
in what you are proposing, it would happen to run under whoever happened to
be the first person to access the application? It just doesn't work that
way.

Whatever you do in application_start can't rely on what user happened to
have made the first request to this app - so it needs a connection
independent of that.

"Ober" <ob**@yahoo.com> wrote in message
news:e0**************@TK2MSFTNGP15.phx.gbl...


I'm having trouble with my security model, Application_Start, and accessing my database.

My ASP.NET app is only going to be running in an intranet environment (not
on the public Internet). The production environment will have installed
everything locally (i.e., IIS, SQL Server, .NET Framework, etc., all on a
Win2k Server).

In IIS, I have disabled anonymous access so that only Integrated Windows
authentication is used. Then in my Web.config file I've set it up to use
Windows authentication, as such:

<appSettings>

<add key="connectionString"
value="database=RegPerfectDb;server=localhost;Trus ted_Connection=true" />

</appSettings>

<system.web>

<authentication mode="Windows" />

<authorization>

<deny users="?" />

</authorization>

<identity impersonate="true" />

</system.web>

Then, in my Application_Start event in Global.asax, I need to run a query or two to get some application-wide data from our database and store it in the application cache.

When I try to access a database from Application_Start, I get the following error:

System.Data.SqlClient.SqlException: Cannot open database requested in login 'DatabaseName'. Login fails. Login failed for user 'MACHINENAME\ASPNET'.

It's as if it is using the default ASP.NET worker process account (which is not what I want). I need it to authenticate/authorize the logged in user
(which has access to the database) and use that user to access the database.


So, my questions are: what am I doing wrong? how do I set up IIS,
Web.config, database connection, etc. so that everything is properly
authenticated/authorized to the logged in user so that I can query the
database in Application_Start?

Thanks in advance for your help.


Nov 18 '05 #2
have a browse through the matrix.
how are you connecting to sql server ? trusted connection ? or sql user /
password ?

http://msdn.microsoft.com/library/de...SecNetAP05.asp

if you are using trusted connection or SSPI then your MACHINENAME\ASPNET
account will have to be granted access to the database (if you want to
enable access from application as there is no user context) plus for normal
user access dont forget to use <impersonate> in web.config

copying from an old code project article (though i wouldnt give access to
ASPNET account as DBOwner i would just just read / write on certain stored
procs
This should work on all other IIS 5.1 (possibly other versions)
combinations. The only difference between IIS 5.1 and IIS 6 is the account
the ASP.NET process runs under. IIS 5.1 runs under a %MACHINENAME%\ASPNET
where %MACHINENAME% is the machine name.

osql -E -S %SERVER%\%INSTANCE% -Q "sp_grantlogin '%MACHINENAME%\ASPNET'"Now
our ASP.NET application will be able to log into the server. Now all thats
left is to grant access to the databases.

osql -E -S %SERVER%\%INSTANCE% -d %DATABASE%
-Q "sp_grantdbaccess '%MACHINENAME%\ASPNET'"
osql -E -S %SERVER%\%INSTANCE% -d %DATABASE%
-Q "sp_addrolemember 'db_owner', '%MACHINENAME%\ASPNET'"These 2 lines will
add access to one of the databases. So if you want to add access to another
database just change %DATABASE% and run both lines.

other way open Query Analyser and do an "EXEC stored proc values here"
--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Ober" <ob**@yahoo.com> wrote in message
news:e0**************@TK2MSFTNGP15.phx.gbl...


I'm having trouble with my security model, Application_Start, and accessing my database.

My ASP.NET app is only going to be running in an intranet environment (not
on the public Internet). The production environment will have installed
everything locally (i.e., IIS, SQL Server, .NET Framework, etc., all on a
Win2k Server).

In IIS, I have disabled anonymous access so that only Integrated Windows
authentication is used. Then in my Web.config file I've set it up to use
Windows authentication, as such:

<appSettings>

<add key="connectionString"
value="database=RegPerfectDb;server=localhost;Trus ted_Connection=true" />

</appSettings>

<system.web>

<authentication mode="Windows" />

<authorization>

<deny users="?" />

</authorization>

<identity impersonate="true" />

</system.web>

Then, in my Application_Start event in Global.asax, I need to run a query or two to get some application-wide data from our database and store it in the application cache.

When I try to access a database from Application_Start, I get the following error:

System.Data.SqlClient.SqlException: Cannot open database requested in login 'DatabaseName'. Login fails. Login failed for user 'MACHINENAME\ASPNET'.

It's as if it is using the default ASP.NET worker process account (which is not what I want). I need it to authenticate/authorize the logged in user
(which has access to the database) and use that user to access the database.


So, my questions are: what am I doing wrong? how do I set up IIS,
Web.config, database connection, etc. so that everything is properly
authenticated/authorized to the logged in user so that I can query the
database in Application_Start?

Thanks in advance for your help.


Nov 18 '05 #3
Interesting. Does this only happen in Application_Start event? Does it
connect to SQL as the logged in user in a later event, say from Page_Load?

Greg
"Ober" <ob**@yahoo.com> wrote in message
news:e0**************@TK2MSFTNGP15.phx.gbl...


I'm having trouble with my security model, Application_Start, and
accessing
my database.

My ASP.NET app is only going to be running in an intranet environment (not
on the public Internet). The production environment will have installed
everything locally (i.e., IIS, SQL Server, .NET Framework, etc., all on a
Win2k Server).

In IIS, I have disabled anonymous access so that only Integrated Windows
authentication is used. Then in my Web.config file I've set it up to use
Windows authentication, as such:

<appSettings>

<add key="connectionString"
value="database=RegPerfectDb;server=localhost;Trus ted_Connection=true" />

</appSettings>

<system.web>

<authentication mode="Windows" />

<authorization>

<deny users="?" />

</authorization>

<identity impersonate="true" />

</system.web>

Then, in my Application_Start event in Global.asax, I need to run a query
or
two to get some application-wide data from our database and store it in
the
application cache.

When I try to access a database from Application_Start, I get the
following
error:

System.Data.SqlClient.SqlException: Cannot open database requested in
login
'DatabaseName'. Login fails. Login failed for user 'MACHINENAME\ASPNET'.

It's as if it is using the default ASP.NET worker process account (which
is
not what I want). I need it to authenticate/authorize the logged in user
(which has access to the database) and use that user to access the
database.

So, my questions are: what am I doing wrong? how do I set up IIS,
Web.config, database connection, etc. so that everything is properly
authenticated/authorized to the logged in user so that I can query the
database in Application_Start?

Thanks in advance for your help.


Nov 18 '05 #4
> Whatever you do in application_start can't rely on what user happened to
have made the first request to this app - so it needs a connection
independent of that.


Ah, that makes good sense. Missed that.


Nov 18 '05 #5
Thanks for the info. That totally makes sense. Do you or does anyone else
have any suggestions on how to do what I'm looking to do? Maybe, put the
code in Session_Start (but then I don't need it to run for every new
session)? Or????
Thanks!
"Marina" <so*****@nospam.com> wrote in message
news:OP**************@tk2msftngp13.phx.gbl...
This is because when the application starts, it isn't running under any
particular user. That only happens for page requests - which is processed
after the application is started. Application_Start runs only once - and so in what you are proposing, it would happen to run under whoever happened to be the first person to access the application? It just doesn't work that
way.

Whatever you do in application_start can't rely on what user happened to
have made the first request to this app - so it needs a connection
independent of that.

"Ober" <ob**@yahoo.com> wrote in message
news:e0**************@TK2MSFTNGP15.phx.gbl...


I'm having trouble with my security model, Application_Start, and accessing
my database.

My ASP.NET app is only going to be running in an intranet environment (not on the public Internet). The production environment will have installed
everything locally (i.e., IIS, SQL Server, .NET Framework, etc., all on a Win2k Server).

In IIS, I have disabled anonymous access so that only Integrated Windows
authentication is used. Then in my Web.config file I've set it up to use Windows authentication, as such:

<appSettings>

<add key="connectionString"
value="database=RegPerfectDb;server=localhost;Trus ted_Connection=true" />
</appSettings>

<system.web>

<authentication mode="Windows" />

<authorization>

<deny users="?" />

</authorization>

<identity impersonate="true" />

</system.web>

Then, in my Application_Start event in Global.asax, I need to run a query or
two to get some application-wide data from our database and store it in

the
application cache.

When I try to access a database from Application_Start, I get the

following
error:

System.Data.SqlClient.SqlException: Cannot open database requested in

login
'DatabaseName'. Login fails. Login failed for user 'MACHINENAME\ASPNET'.

It's as if it is using the default ASP.NET worker process account (which

is
not what I want). I need it to authenticate/authorize the logged in

user (which has access to the database) and use that user to access the

database.



So, my questions are: what am I doing wrong? how do I set up IIS,
Web.config, database connection, etc. so that everything is properly
authenticated/authorized to the logged in user so that I can query the
database in Application_Start?

Thanks in advance for your help.



Nov 18 '05 #6
Is there any reason why you can't create a login for the ASPNET user?

Also, it is recommended that you use a single account to access your
database rather than use an account for each user. That way you will be
using your pooled connections more efficiently.

- Frank Mamone

"Ober" <ob**@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Thanks for the info. That totally makes sense. Do you or does anyone else have any suggestions on how to do what I'm looking to do? Maybe, put the
code in Session_Start (but then I don't need it to run for every new
session)? Or????
Thanks!
"Marina" <so*****@nospam.com> wrote in message
news:OP**************@tk2msftngp13.phx.gbl...
This is because when the application starts, it isn't running under any
particular user. That only happens for page requests - which is processed
after the application is started. Application_Start runs only once - and
so
in what you are proposing, it would happen to run under whoever happened to
be the first person to access the application? It just doesn't work that
way.

Whatever you do in application_start can't rely on what user happened to
have made the first request to this app - so it needs a connection
independent of that.

"Ober" <ob**@yahoo.com> wrote in message
news:e0**************@TK2MSFTNGP15.phx.gbl...


I'm having trouble with my security model, Application_Start, and

accessing
my database.

My ASP.NET app is only going to be running in an intranet environment

(not on the public Internet). The production environment will have installed everything locally (i.e., IIS, SQL Server, .NET Framework, etc., all on a
Win2k Server).

In IIS, I have disabled anonymous access so that only Integrated
Windows authentication is used. Then in my Web.config file I've set it up to

use Windows authentication, as such:

<appSettings>

<add key="connectionString"
value="database=RegPerfectDb;server=localhost;Trus ted_Connection=true" />
</appSettings>

<system.web>

<authentication mode="Windows" />

<authorization>

<deny users="?" />

</authorization>

<identity impersonate="true" />

</system.web>

Then, in my Application_Start event in Global.asax, I need to run a query
or
two to get some application-wide data from our database and store it in the
application cache.

When I try to access a database from Application_Start, I get the

following
error:

System.Data.SqlClient.SqlException: Cannot open database requested in

login
'DatabaseName'. Login fails. Login failed for user
'MACHINENAME\ASPNET'.
It's as if it is using the default ASP.NET worker process account

(which is
not what I want). I need it to authenticate/authorize the logged in

user (which has access to the database) and use that user to access the

database.



So, my questions are: what am I doing wrong? how do I set up IIS,
Web.config, database connection, etc. so that everything is properly
authenticated/authorized to the logged in user so that I can query the
database in Application_Start?

Thanks in advance for your help.




Nov 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Greg | last post by:
I am working on a project that will have about 500,000 records in an XML document. This document will need to be queried with XPath, and records will need to be updated. I was thinking about...
3
by: Mike Dundee | last post by:
I am importing data into a new database (the database still has to be set up) and have a problem. The comma delimited text files I am importing have four fields containing date and date/times. ...
6
by: Leslie | last post by:
I am attempting to handle errors by using Application_Error. This seems to work fine in most situations. However, if the exception occurs during the Application_Start method, the stand error...
4
by: Marc Missire | last post by:
Hi, I have an issue below I'd love help with, involving a static variable, Application_Start, and a background thread. In global.asax.cs I have a static variable (outside any method) with a...
8
by: bryan | last post by:
Is there any way I can get the application path (the one returned by Request.ApplicationPath) in the Application_Start method in Global.asax? Request is not valid there. On a related note, is there...
2
by: Rubber Steve | last post by:
I am having trouble querying an Access database with ADO .NET in VB .NET. I can get results returned using primary keys, but when using something like LastName, nothing happens. I am not sure why...
7
by: Christian Blackburn | last post by:
Hi Gang, Let me start by saying I'm using Visual Web Developer 2005 and ASP.net 2.0. Is there something I have to do to get my Global.asax fire when my application loads. If I set a breakpoint...
2
by: RajSharma | last post by:
Hi, I am facing a problem regarding querying thru a large table having millions of rows....... Its hanging in between while querying for all those rows Can anybody suggest me a query regarding :...
4
by: bhughes2187 | last post by:
I am using access 2003, I am querying the table scarlet using a statement sql1 = "select rpt from scarlet where rpt <> '~' group by rpt" basically, rpt will have values of ~, A, B, C, D. A can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.