0

I have a ASP.NET web application, written by the use of Razor pages and .Net 6. When I was working on my own pc, every thing was OK. Now, I should take the web application to the server computer in a way that many users can use the application.

I published the application into a folder and take that to the server computer.

On the server computer, I use IIS and add a new website which refers to the published folder of application as physical path.

on a client computer, when I go to the website address of the application, the html page and css styles all are OK, but I got the error:

Login failed for user 'ERTEBATCITFOOD$'.

I also provide a piece of my asp.net code for more information. For example, this is what I wrote for connecting to database and making a query:

string connectionString = "Data Source=citfood;Initial Catalog=food;Integrated Security=True";

    try
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            string sql1 = "SELECT date, foodName, price FROM [View_1] WHERE userId = @userId " +
                             "AND date = @thisDate";

                using (SqlCommand command = new SqlCommand(sql1, connection))
                { 
                    //do something
                }
}
}

I don't know what is wrong here...

mason
  • 31,774
  • 10
  • 77
  • 121
Nastaran
  • 91
  • 7
  • Please post the full error details. You left out the actual name of the exception and the stack trace. Those are important. Also, indicate which line in the code you provided the error came from. And since it's a login issue and you're doing integrated security, did you verify that the web server is able to connect to the DB server via DNS? Are they on the same Windows domain? Does the account that the application pool is running as have permissions to the database server? There's a lot of debugging questions here you should anticipate and include in your question. – mason Jan 31 '23 at 14:39
  • Typical misunderstanding of "my own pc". There are tons of key differences, https://halfblood.pro/web-application-differences-in-visual-studio-and-iis-60fec7e311b3 so that database connection as well as many other pieces can break. – Lex Li Jan 31 '23 at 16:53
  • It is difficult to reproduce your problem based on your description, you say html page and css styles all are OK, Is there an error in the content of the page? from your description, it seems to be related to the database. You can use the related errors here as a reference: [link](https://stackoverflow.com/questions/7698286/login-failed-for-user-iis-apppool-asp-net-v4-0). – samwu Feb 01 '23 at 08:50

1 Answers1

1

I understand your problem. This error happened when an attempt to connect to database server fails. You should place your "connectionString " in the "appsettings.json" file. Just add this line to your "appsettings.json" file.

"ConnectionStrings": {
"WinAuth": "Data Source=citfood; Initial Catalog=food; Integrated Security=True",
"SQLAuth": "Data Source=citfood; Initial Catalog=food; User ID=sa;Password=ENTERYOURPASSWORD!"

}

And then use one of the "WinAuth" or "SQLAuth" to connect to the database. (Remember to put the true ID and Password values!) Have fun!

nastaran
  • 132
  • 1
  • 12
  • Keep in mind clear password in `appsettings.json` is a clear risk and secret management best practices should be considered. – Lex Li Mar 04 '23 at 18:59
  • @Lex Li So what to do? – Nastaran Mar 06 '23 at 13:25
  • @Nastaran Unfortunately it is rather lengthy to summarize, so I just leave some hints to help you get started. For development machines, you can follow https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-7.0&tabs=windows For production environment, you'd better use container secret management or things Azure Key Vault. – Lex Li Mar 07 '23 at 01:56