hosting ASP.NET Core web app on windows server - asp.net-core

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...

Related

Microsoft SQL Server 2014 Error: 18452? [duplicate]

My webpages are on secured server (https), and I am trying to connect the SQL Server 2008 Database, which is normal server.
I am writing connectionstring on page itself, not in web.config file. And I am getting following error:-
System.Data.SqlClient.SqlException: Login failed.
The login is from an untrusted domain and cannot be used with Windows authentication.
Please help, how can I connect it, does I have to make some webservices for it.
my code is as below:
public void FillCity()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "integrated security=SSPI;data source=dev-fcb; user id=sa;password=password;"
+"persist security info=False;database=mediapro";
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select * from StateCityMaster where IsActive='1' order by CityName", con);
DataSet ds = new DataSet();
da.Fill(ds);
string CityName = string.Empty;
if (ds.Tables[0].Rows.Count > 0)
{
CityName = ds.Tables[0].Rows[0]["CityName"].ToString();
}
DataSet dset = new DataSet();
da.Fill(dset);
if (dset.Tables[0].Rows.Count > 0)
{
drpCity.DataSource = dset;
drpCity.DataTextField = "CityName";
drpCity.DataValueField = "CityName";
drpCity.DataBind();
}
drpCity.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
}
Your connection string is telling it to use integrated security SSPI, which will use the Windows credentials.
Set Integrated Security to false if you are going to be providing the username and password.
Also, consider putting your connection string inside of the web.config file - it is more secure and reusable.
From http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=VS.100).aspx:
When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.
Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true.
If User ID and Password are specified and Integrated Security is set to true, the User ID and Password will be ignored and Integrated Security will be used.
I created a new Asp.Net Core MVC site and I had this same error. I was attempting to connect to my company's database while connected to the network via VPN. Here's what worked for me:
Instead of
"DevConnection": "Server=DevDBServer;Database=MyDatabase;User ID=some_userid;Password=some_password;Integrated Security=SSPI;Trusted_Connection=True;"
I used
"DevConnection": "Server=DevDBServer;Database=MyDatabase;User ID=some_userid;Password=some_password;Integrated Security=False;Trusted_Connection=False;"
The key change was to make sure that Trusted_Connection=False, which is consistent with the error message. Generally I would not use an untrusted connection. In this case I was connecting to a dev/test database so it's fine.
For future googlers:
If you do need Integrated Security and are getting this error it might be you're using a local account instead of a domain account.
I came across this running Visual Studio locally and trying to connect to a database on another machine. A workaround was to run Visual Studio as a different user, the prompt didn't work but running the command below did (make sure to replace DOMAIN\USER and you will be asked to provide credentials):
runas /netonly /user:DOMAIN\USER "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\devenv.exe"
(This is for VS2019, your path may vary).
Old question, and my symptoms are slightly different, but same error. My connection string was correct (Integrated security, and I don't provide user and pwd) with data source set to 127.0.0.1. It worked fine for years.
But recently I added a line in the static host file for testing purposes (C:\Windows\System32\drivers\etc\hosts)
127.0.0.1 www.blablatestsite.com
Removing this line and the error is gone.
I got a clue from this article (https://support.microsoft.com/en-gb/kb/896861) which talks about hostnames and loopback.
Other possible fix (if you need to keep that line in the hosts file) is to use the hostname (like MYSERVER01) instead of 127.0.0.1 in the data source of the connection string.
I had this same issue while accessing this through work where we use Azure authentication - I'd changed my password through the Azure password reset service but this only pushed through after I manually updated the password on the schema settings in my RDBMS (in my case, DataGrip).

Login failed. The login is from an untrusted domain and cannot be used with Windows authentication

My webpages are on secured server (https), and I am trying to connect the SQL Server 2008 Database, which is normal server.
I am writing connectionstring on page itself, not in web.config file. And I am getting following error:-
System.Data.SqlClient.SqlException: Login failed.
The login is from an untrusted domain and cannot be used with Windows authentication.
Please help, how can I connect it, does I have to make some webservices for it.
my code is as below:
public void FillCity()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "integrated security=SSPI;data source=dev-fcb; user id=sa;password=password;"
+"persist security info=False;database=mediapro";
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select * from StateCityMaster where IsActive='1' order by CityName", con);
DataSet ds = new DataSet();
da.Fill(ds);
string CityName = string.Empty;
if (ds.Tables[0].Rows.Count > 0)
{
CityName = ds.Tables[0].Rows[0]["CityName"].ToString();
}
DataSet dset = new DataSet();
da.Fill(dset);
if (dset.Tables[0].Rows.Count > 0)
{
drpCity.DataSource = dset;
drpCity.DataTextField = "CityName";
drpCity.DataValueField = "CityName";
drpCity.DataBind();
}
drpCity.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
}
Your connection string is telling it to use integrated security SSPI, which will use the Windows credentials.
Set Integrated Security to false if you are going to be providing the username and password.
Also, consider putting your connection string inside of the web.config file - it is more secure and reusable.
From http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=VS.100).aspx:
When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.
Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true.
If User ID and Password are specified and Integrated Security is set to true, the User ID and Password will be ignored and Integrated Security will be used.
I created a new Asp.Net Core MVC site and I had this same error. I was attempting to connect to my company's database while connected to the network via VPN. Here's what worked for me:
Instead of
"DevConnection": "Server=DevDBServer;Database=MyDatabase;User ID=some_userid;Password=some_password;Integrated Security=SSPI;Trusted_Connection=True;"
I used
"DevConnection": "Server=DevDBServer;Database=MyDatabase;User ID=some_userid;Password=some_password;Integrated Security=False;Trusted_Connection=False;"
The key change was to make sure that Trusted_Connection=False, which is consistent with the error message. Generally I would not use an untrusted connection. In this case I was connecting to a dev/test database so it's fine.
For future googlers:
If you do need Integrated Security and are getting this error it might be you're using a local account instead of a domain account.
I came across this running Visual Studio locally and trying to connect to a database on another machine. A workaround was to run Visual Studio as a different user, the prompt didn't work but running the command below did (make sure to replace DOMAIN\USER and you will be asked to provide credentials):
runas /netonly /user:DOMAIN\USER "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\devenv.exe"
(This is for VS2019, your path may vary).
Old question, and my symptoms are slightly different, but same error. My connection string was correct (Integrated security, and I don't provide user and pwd) with data source set to 127.0.0.1. It worked fine for years.
But recently I added a line in the static host file for testing purposes (C:\Windows\System32\drivers\etc\hosts)
127.0.0.1 www.blablatestsite.com
Removing this line and the error is gone.
I got a clue from this article (https://support.microsoft.com/en-gb/kb/896861) which talks about hostnames and loopback.
Other possible fix (if you need to keep that line in the hosts file) is to use the hostname (like MYSERVER01) instead of 127.0.0.1 in the data source of the connection string.
I had this same issue while accessing this through work where we use Azure authentication - I'd changed my password through the Azure password reset service but this only pushed through after I manually updated the password on the schema settings in my RDBMS (in my case, DataGrip).

Reports Timeout when Opening From ASP.Net ReportViewer

I use this c# code for opening reports from SharePoint 2010 site and SQL Server Reporting Services 2012.
if (!IsPostBack)
{
ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
ReportViewer1.ServerReport.ReportServerUrl = new Uri("https://site.com/_vti_bin/reportserver");
ReportViewer1.ServerReport.ReportPath = "https://site.com/sites/ssrs/Reports/Report1.rdl";
ReportViewer1.ShowParameterPrompts = false;
ReportViewer1.ShowCredentialPrompts = false;
IReportServerCredentials irsc = new ReportViewerCredentials("user", "pass", "domain");
ReportViewer1.ServerReport.ReportServerCredentials = irsc;
ReportViewer1.ServerReport.Refresh();
}
I can open the reports from SharePoint site, but when I try to open them using the above code in ASP.Net page they timeout. The code works fine since I have succeed to open reports from my test farm. In my production farm, I get the message:
"The underlying connection was closed: The connection was closed unexpectedly."
The interesting thing is the reports opened twice (in two days) and when I tried to open other reports, it just failed like before. This happened when I restated IIS, but it doesn't happen again.
I have all the permissions on the server and SharePoint Farm. The report is just a page with a line on it, it doesn't even have a data source.

SQL Server 2005, Windows Authentication, Connection String giving error "Login Failed"

I have SQL Server 2005 and Visual Studio 2005 installed on my desktop.
Every time I run SQL Server 2005, I have to go for "RUN AS ADMINISTRATOR" manually. I have tried changing the settings but no go. So I do it that way only.
But anyways, that's not the actual issue.
Now when I am making a connection string in Visual Studio- console application for Windows Authentication, it is throwing an error "Login failed".
I am new to .NET
Code is as below:
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
public class Vin
{
public static void Main()
{
Console.WriteLine("Hello World!!!");
SqlConnection con = new SqlConnection("Data Source=.; Initial Catalog=VinOne; Integrated Security=True");
SqlCommand cmd = new SqlCommand("select * from Prod", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Console.WriteLine("Got It!!!");
}
Console.Read();
}
}
the SqlConnection object that you are creating is probably incorrect.. The syntax should be something like below:
SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial Catalog=VinOne;Integrated Security=SSPI");
SSPI stands for Security Support Provider Interface (if you are curious)
Also check what user the service runs as, to do this navigate to Run. Type Services.msc and then navigate to SQL server, see the value for Log On As and set it to your currently logged in or whatever user Visual Studio runs under, I am guessing the SQL server currently requires higher privileges.
Hope that works! Good luck..
I came to know that this strange situation can be resolved by running the Visual Studio in Admin Mode (by right clicking on the icon and selecting 'Run as Admin") as I was doing with SQL Server 2005.
Thanks a lot to all for your support and guidance.

Entity Exception : the underlying provider failed to open

iv'e got a wcf service host hosting a service on IIS7,
iv'e added a database to it's App_Data folder ,
the service is referenced to a DAL project
which holds an Entity Framework model generated from my DB ( The DB from the WCF Service Host )
i keep getting the above entity exception with this inner message :
{"An attempt to attach an auto-named database for file C:\\Users\\eranot65\\Documents\\Visual Studio 2010\\Projects\\CustomsManager\\WcfManagerServiceHost\\App_Data\\CustomesDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."}
iv'e copied the connection string from DAL/app.config to WcfManagerServiceHost/Web.config
add name="CustomesDBEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\eranot65\Documents\Visual Studio 2010\Projects\CustomsManager\WcfManagerServiceHost\App_Data\CustomesDB.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True'" providerName="System.Data.EntityClient"
this happens when i try to use my data source entity model:
public List<Employee> GetEmployees()
{
List<Employee> employees = null;
using (CustomesDBEntities entites = new CustomesDBEntities())
{
employees = entites.Employees.ToList<Employee>();
}
return employees;
}
it doesn't seem as if the DB is in use some where else ,
(1) how can i check if some other process is holding a handle to my DB ?
(2) in ideas this happens ?
I would consider checking one of two things:
Create a connection to your SQL Express, either in VS server explorer, or by using the SQL management studio, and verify you do not already have a database by that name attached to your server.
Move your project from it's current location to somewhere on the disk which is not user-specific (meaning not on the desktop, documents etc..), for example - c:\temp, c:\projects... The reason for that is that you are running a web application, and in case you run it in IIS, the identity of the worker process is a special identity other than yours which might not have permissions to access the database file since it is located in a private folder of your user
Most likely the problem is that you are opening the database with Visual Studio and your application at the same time. The connection string explicitely configures AttachDbFilename=... AttachDBFilename spins up a user instance of SQL Express attached to a specific DB Filename for single user mode. In single user mode, only one application can open the MDF at a time.
well the answer is always simpler then you would think
all i ended up doing is changing to automatically generated connection string
generated by the EntityFramework model , to a connection string to locate the DB in my App_Data folder
my original connection string :
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='Data
Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\eranot65\Documents\Visual Studio 2010\Projects\CustomsManager\WcfManagerServiceHost\App_Data\CustomesDB.mdf"
;Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True'" providerName="System.Data.EntityClient"
my edited connection string :
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;
provider connection string='Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CustomesDB.mdf;Integrated Security=True;;User Instance=True;MultipleActiveResultSets=True'"