ASP.NET web application is forcefully connecting to SQL Server database using Windows authentication

tjvv9vkg  于 2023-04-19  发布在  .NET
关注(0)|答案(3)|浏览(189)

I have an asp.net core web application. Here I am trying to get data from my SQL Server. for that I am configuring the appsettings.json file with below text.

{
    "Logging": {
        "LogLevel": {
        "Default": "Warning"
    }
},
"AllowedHosts": "*",
"ConnectionStrings": {
    "QuickKartDBConnectionString": "data source=MyServerName;initial catalog=ProductDB;Integrated security=False;User ID=User1;Password=User1@123;"
}

While debugging the code I found it is trying connecting to ProductDB using Windows login. I am getting this error:

Cannot open database "ProductDB" requested by the login. The login failed.
Login failed for user 'mydomain\kcs.sahoo'

Please help me to sort out the issue.

Thanks in advance!

wxclj1h5

wxclj1h51#

you can use this connection string for connect with Windows login

Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ProductDB;Data Source=.

you can use this connection string for connect with user sql

Password=User1@123;Persist Security Info=True;User ID=User1;Initial Catalog=ProductDB;Data Source=.

status Server authentication in security is sql server and windows authentication mode

way change Server authentication to Mixed Authentication(sql server and windows authentication mode)

Log in to the Microsoft SQL Server Management Studio with a predefined user account, or if one was not set up for SQL authentication, use Windows Authentication.

Right-click the server you wish to modify and then click Properties.

Select the Security Page.

Under the Server authentication heading choose either the desired authentication: Windows Authentication or SQL Server and Windows Authentication mode.

Click OK.

At this point the SQL server must be restarted. To do so, right-click the server you have just modified and select Restart.

If SQL Server Agent is running, it must also be restarted.

lhcgjxsq

lhcgjxsq2#

There are a few things that you're going to want to check:

  1. Are you sure that you've set the correct connection string in your DbContext when you set it up?
public static IServiceCollection SetupSql(this IServiceCollection services, IConfiguration config)
{
    var connectionString = config.GetConnectionString("QuickKartDBConnectionString");

    services.AddDbContext<YourContext>(options => options.UseSqlServer(connectionString));

    return services;
}
  1. VS generates two appsettings.json files: one for production and one for development. Check to make sure that you updated the value in appsettings.development.json for your connection string.

0qx6xfy6

0qx6xfy63#

You need this part Integrated Security=SSPI in connection string for windows authentication.

You could try configure your db with SQL Server and windows authentication mode

Last but not least,not store your secrets in appsettings.json,check this document related with secret storage

相关问题