Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using IIS 7 and I have the site in IIS and I created the virtual directory. Then I converted it to an application. I then browsed it and my login page I made came up. I went to login and I get an error:

CSS
Server Error in '/sacsapp' Application.
Login failed for user 'SACS-COC\SPARE-PC$'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Login failed for user 'SACS-COC\SPARE-PC$'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


When I run the program in VS everything works. Why is that? What did I do wrong? There is a database the web app is connected also. Can Someone please help?!!
Posted
Comments
Computer Wiz99 19-Sep-13 12:02pm    
Ok. I am running IIS 7 on my local computer where VS is also located on. I have not moved anything to a server yet. The only thing that is on a server is the SQL Database. Any reason why when I run the web app in IIS 7 I can get to the login page but when I login I get the error message I have?
mgoad99 19-Sep-13 12:07pm    
When you run in visual studio, it runs under the account logged onto the pc. When you run from IIS7 (regardless of if on a server or your local pc) the web app runs under that local account SACS-COC\SPARE-PC$ in your case. That account will not normally have access to your database. See my solution below. Hope that helps

This is one of the pains of running in visual studio for testing. Things work fine, but then when moved to IIS7 some things may break.

1 solution

I tried submiting this as a comment, but the code did not display, lets try it as a solution. :)

When you run in visual studio, your app runs under the account you are logged onto your computer as. That is the account that will be used to access your database. For me, i am usually logged in as an adminstrator with full access to my databases. The same may be true for you.

Now, when you set things up in IIS, by default (unless you have done something else), the web app will run under a local account on that server. Which sounds like the case for you since the error message says: login failed for user SACS-COC\SPARE-PC$. That is the account that is trying to access your database, which probably doesn't have access.

What i normally do is in the web.config (where i keep connection strings), I pass a username and password for a SQL account that has access to the database. Like this:
<add name="MyConnectionString" connectionstring="Data Source=<ServerName>;Initial Catalog=<DB Name>;Persist Security Info=True;User ID=<UserName>;Password=<Password>">
   providerName="System.Data.SqlClient" /></add>


This isn't the most secure since you will have plain text username and password in the web.config file. There are ways to encrypt and other ways to do this too. But, i hope this gives you enough info on WHY the error is happening and points you in a direction to find a solution that works for you.
 
Share this answer
 
Comments
Computer Wiz99 19-Sep-13 12:10pm    
Thanks. I will give it a shot. I just need to add the user sacs-coc\spare-pc to permissions and that will work you think?
mgoad99 19-Sep-13 12:26pm    
You can try that. My guess will be that that is a local account on your pc. Which means that the server will not accept that. SQL Server will only be able to "see" domain accounts.

Like i said, i normally create a new SQL account and then put that in the connection string.

But, as i understand here are the options you have
1) create a SQL account and hardcode the user name and password in your connection string (as i explained above). If this is in the web.config file and you are concerned about security, you can encrypt this. I have done that with one intranet app i have. There are a lot of articles on how to encrypt.
http://stackoverflow.com/questions/2289230/opening-sql-connection-from-iis7

2) Setup your app in the web.config file to impersonate a domain account. This means the web app will no longer run under SACS-COC\SPARE-PC$, but some domain account of your choosing that has access to the database. Again, username and password will be in the web.config and you may want to encrypt that.
<identity impersonate="true" username="domain\username" password=" password">
http://www.codeproject.com/Articles/107940/Back-to-Basic-ASP-NET-Runtime-Impersonation

You will need to do some research to figure out what will work best for you.
Computer Wiz99 19-Sep-13 13:00pm    
Ok. Thanks. What about putting this on a server that has IIS with a web site already on the web? I will just have to add the ASP.NET web app that I made as a VD under that site? Will the users I give access to be able to login or will the same error happen?
mgoad99 19-Sep-13 13:12pm    
Yep, to move it to a server, the setup should be the same as what you did on your local pc. Yes, i think you will still have the same error with the database connection.
Computer Wiz99 19-Sep-13 13:41pm    
Ok. So if everyone can get to the website and they click on the link to the login page then the ones who has the username and password will not be able to get in?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900