59

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"
      }
    }
  }

7 Answers 7

105

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/

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

Comments

51

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;"
  }

Comments

5

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;"

Comments

4

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

Comments

2

Try this:

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

Comments

2

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"
                );
            }
        }

Comments

-3

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

1 Comment

No, the issue was that the connection string attribute key is "User Id" (with a space), not "UserId"

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.