nobody
nobody

Reputation: 11090

Setting connection string with username and password in ASP.Core MVC

I am working on my first ASP.NET Core MVC application.What is the right way to specify the connection string in a ASP.NET Core MVC application with a sql server backend requiring sql authentication?

ArgumentException: Keyword not supported: 'userid'.\

Below is my appsettings.json file. When I run the application, it throws an error.

{
  "ApplicationInsights": {
    "InstrumentationKey": ""
  },
  "ConnectionStrings": {
      "DefaultConnection": "Server=myserver;Database=mydatabase;userid=id;password=mypwd"
  },
    "Logging": {
      "IncludeScopes": false,
      "LogLevel": {
        "Default": "Debug",
        "System": "Information",
        "Microsoft": "Information"
      }
    }
  }

Upvotes: 58

Views: 232330

Answers (7)

Jeremy Cook
Jeremy Cook

Reputation: 22073

User Id is the correct way, with space in the middle

Server=myServerAddress;Database=myDataBase;User Id=myUsername; Password=myPassword;

https://www.connectionstrings.com/sql-server/

Upvotes: 103

RainyTears
RainyTears

Reputation: 189

Please write bellow code in dbcontext file

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer(
                    @"Server =.; Database = MyDb; User id=uid; Password=pwd; Trusted_Connection = True; MultipleActiveResultSets = true"
                );
            }
        }

Upvotes: 2

Dev
Dev

Reputation: 1792

Please try this followingcode one: with latest Visual Studio 2022 it works

"ConnectionStrings": "Server=ServerAddress;Database=DatabaseName;User Id=User;password=Password;Trusted_Connection=SSPI;Encrypt=false;TrustServerCertificate=true;MultipleActiveResultSets=true;"

Upvotes: 5

Miguel Linares
Miguel Linares

Reputation: 29

Try this:

"ConnectionStrings": {
    "MvcMovieContext": "Server=localhost\\NOMINSTANCE;Database=NomBD;Trusted_Connection=True;MultipleActiveResultSets=true;User ID=sa;Password=password1;Integrated Security=False"
}

Upvotes: 2

Majid joghataey
Majid joghataey

Reputation: 1537

That's because the correct keyword is User Id, as shown in the following example:

"ConnectionStrings": {
    "DefaultConnection": "Server=myServerAddress;Database=myDataBase;User Id=myUsername;password=myPassword;Trusted_Connection=False;MultipleActiveResultSets=true;"
  }

Upvotes: 50

Vinod kumar
Vinod kumar

Reputation: 35

By setting Integrated security = false in your connection string in web.config file then this error will be solved

Upvotes: -2

Mickaël Derriey
Mickaël Derriey

Reputation: 13704

Can you try User ID instead of userid?

Note the main difference is in the space between user and id, the case doesn't matter

Upvotes: 4

Related Questions