0
int userId = 0;

string roles = string.Empty;

string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

using (SqlConnection con = new SqlConnection(constr))
{
    using (SqlCommand cmd = new SqlCommand("Validate_User"))
    {
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@Username", Login1.UserName);
        cmd.Parameters.AddWithValue("@Password", Login1.Password);
        cmd.Connection = con;

        con.Open();

in the web.config, the connection string is defined as

<add name="constr" 
     connectionString="data source=.;initial catalog=LoginDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" 
     providerName="System.Data.SqlClient" />

but when I run it, I get an error:

System.Data.SqlClient.SqlException: Cannot open database "LoginDB" requested by the login. The login failed.
Login failed for user 'NT AUTHORITY\SYSTEM'.

Any solutions?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Senal
  • 13
  • 4
  • Possible duplicate of [Cannot open database "test" requested by the login. The login failed. Login failed for user 'xyz\ASPNET'](https://stackoverflow.com/questions/2575907/cannot-open-database-test-requested-by-the-login-the-login-failed-login-fail) – Liam Mar 20 '19 at 13:23

2 Answers2

1

As you use integrated security, you must either give the default system user rights to connect to the DB (not a good idea) or configure the application pool the web app is using to run under a specific user which has the required rights on the DB. A third option would be not to use integrated security and store the SQL username and password in the connection string. But integrated security is the best way if you can use it. HTH

Fred Mauroy
  • 1,219
  • 1
  • 11
  • 10
0

You can give NT AUTHORITY\SYSTEM user permission in SQL Server, which I don't recommend.

Another way is to use SQL Server Authentication and specify the User Id and Password

<add name="constr" providerName="System.Data.SqlClient" connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=False;User Id=userid;Password=password;MultipleActiveResultSets=True" />
ElasticCode
  • 7,311
  • 2
  • 34
  • 45
  • yes thanks but i tried that but then i get an error like this .. Could not load file or assembly 'MySql.ConnectorInstaller, Version=6.10.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The system cannot find the file specified. – Senal May 06 '18 at 21:04
  • thats just it .. iv used VS with mysql in previous days .. now when i open this currnt project whch uses sql server that meessage comes – Senal May 07 '18 at 06:41
  • Please share a screenshot for project references – ElasticCode May 07 '18 at 22:39