1

I am trying to connect my .NET Core application to SQL Server using a connection string. However it is failing and I get this error:

login failed for user

This is the connection string I am using

"ConnectionStrings": {
    "DefaultSQLConnection": "Server=DESKTOP-User\\MSSQLSERVER;Database=VillaDatabase;TrustServerCertificate=True;"
}

Below is the line of code I am using in my Program.cs file:

builder.Services.AddDbContext<ApplicationDbContext>(option =>
{       
     option.UseSqlServer(builder.Configuration.GetConnectionString("DefaultSQLConnection"));
});

I am currently using .NET Core 7.

Can anyone please tell me what could be the fix for this?

4
  • 1
    Please don't overwrite people's changes. Commented Dec 16, 2022 at 5:18
  • 1
    "login failed for user" - the error is pretty self-explanatory and you haven't specified any form of username or trusted connection. Depending on how you are hosting your ASP.NET Core website, the server will most likely be running it in the equivalent to IIS_IUSRS which most likely hasn't been granted access to your DB. Either run the website in an explicit account or specify SQL Username+password in your config. Commented Dec 16, 2022 at 5:21
  • @Deleted I was trying using window authentication. DO i still need username for that? Commented Dec 16, 2022 at 14:46
  • No, but you have insufficient config even for WA Commented Dec 17, 2022 at 3:26

2 Answers 2

1

We do in our application as:

  1. Add connection string in appsetting.json
"connectionString": "data source=dbserver;initial catalog=dbname;user id=dbuserid;password=dbpassword;MultipleActiveResultSets=True;Encrypt=True;Connection Timeout=60;",
  1. Read in c# code as
    var configuration = new ConfigurationBuilder()
                        //.SetBasePath(Directory.GetParent(Directory.GetCurrentDirectory()) + "/ProjectName").AddJsonFile("config.json", false)
                        .SetBasePath(Directory.GetCurrentDirectory() + "/").AddJsonFile("appsettings.json", false)
                        .AddJsonFile($"appsettings.{SessionModel.Environmentname}.json", optional: true, reloadOnChange: true)
                        .Build();
    string StaticConnectionString = configuration.GetSection("connectionString").Value;

Make sure the proper path of appsetting.json you get in above code. For your info, its added at project level (you can find near about startup.cs file)

Sign up to request clarification or add additional context in comments.

Comments

0

Can anyone please tell me what could be the fix for this.

In your Server Name which is DESKTOP-User\\MSSQLSERVER you should use your server name, not user. You can get the server information as below:

enter image description here

Note: First, try to login in SSMS using user name and password then use the same in your connection string.

Please refer to following code snippet.

Program.cs:

var connectionString = builder.Configuration.GetConnectionString("DefaultSQLConnection");
builder.Services.AddDbContext<ApplicationDbContext>(x => x.UseSqlServer(connectionString));

Appsetting.js When Login As User:

"ConnectionStrings": {
    "DefaultSQLConnection": "Server=YourServerName;Database=YourDatabaseName; User Id=User;password=Password; MultipleActiveResultSets=true"
  },

Appsetting.js When Use Windows Authentication:

"ConnectionStrings": {
        "DefaultSQLConnection": "Server=YourServerName;Database=YourDatabaseName; Trusted_Connection=True;MultipleActiveResultSets=true"
      },

Note: If you want to access database as an perticular user in that scenario include these two parameter User Id=User;password=Password;. In addition, if you would use perticular user in that case yon can remove Trusted_Connection=True but for windown authentication you could it to true.

If you need further details you could refer to our official document and also this document

4 Comments

I agree, As I am not sure, op's environment thus didn't explain, I will update the answer for sure. Thank you.
@Deleted Updated sir, now seems perfect.
thanks , the string as a User worked for me. However i had to add additional parameter of TrustedCertificate=True to the string. However Window Authentication is failing for the string though i am able to login to sql server using window authentication
Thanks for your response, glad to assist you on this, I hope it resolved your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.