Setting Connection String With Username and Password in Asp.Core MVC

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

user<space>id

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

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

How to specify a username using connection string in Entity Framework

You will need to enable sql authentication mode, add the user to the sql server logins and specify the password in the connection string.

Example connection string:

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

(source)

Eanabling sql authentication mode:

enter image description here

ASP.NET Core MVC Changing Connection String does not work

If you are using EFCore you may have this in your DbContext:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
optionsBuilder.UseSqlServer(
"sqlconnstring");
}

Might just need to update it there mate.

How to set username and password in connection string for connecting to database?

Well the problem is right there in the stack.

Login failed for user 'WIN-J9A67LA4GER\hostusername_web'

You are setting Integrated Security=True and also specifying a username and password. From my understanding Integrated Security=True will use the Windows Credentials that the website is running under. As answered here.

If you want to specify a SQL Login in your connection string then you need to set that to False.

Having Connection String for Development and Production

Create file named appsettings.development.json in the same folder that standard appsettings.json is located.

Content of this file should be 1-1 copy of original appsettings.

In original appsettings modify the connection string, so it will point to live database. You can remove unused settings that will not be changed for production environment from appsettings.development.json.

Set Your production server's ASPNETCORE_ENVIRONMENT system variable to Production (optional, at least in theory).

You will find more info in here.

How to secure a Connection String in ASP.NET using tokens?

No, it is not possible. It's either a user/pass or "integrated security". The integrated security is similar to Windows Auth, in that it sort automatically logs you in to the DB. However, just as with Windows Auth, there are prerequisites. The client (your app) must be on the same domain as your DB server, i.e. they must both be joined to a Windows domain, and be on the same network or connected via a virtual private network. If the database is truly "external", then you cannot use this.

There's absolutely nothing wrong with including the username and password in the connection string. However, you should then of course take steps to protect the connection string. First, it should never be hardcoded, but always provided via configuration. Then, it should be in a configuration source that is relatively secure. For example, don't drop it in something like appsettings.json, as it's both not encrypted and will end up committed to your source control. Instead, you should use environment variables or secure secret store like Azure Key Vault. Environment variables are not encrypted, but they are local on the server and can be protected via user-level access controls. While there's potential to be exposed, it would require a malicious actor to have relatively high-level access to log in directly to your server, which means that the exposed connection string is really the least of your problems at that point.

Password with special characters in connectionString

In this example I see two things:

  1. A & in xml should be escaped with & (Good explanation in this answer)
  2. A ; in a connection string, you should wrap the password in single quotes

So this should work for you:

<connectionStrings>
<add name="CRM365" connectionString="AuthType=AD;Url=http://crm.xxx.com/CRM365; Domain=test; Username=test; Password='T,jL4O&vc%t;30'" />
</connectionStrings>

Edit (just tried it for myself):

Additionally, another variant is to use escaped double quotes:

<connectionStrings>
<add name="CRM365" connectionString="AuthType=AD;Url=http://crm.xxx.com/CRM365; Domain=test; Username=test; Password="T,jL4O&vc%t;30"" />
</connectionStrings>

Summary:

Use either password='T,jL4O&vc%t;30'; or password="T,jL4O&vc%t;30";

ConnectionString in .NET Core 6

Problem is how you start your Web API from the service. You are using Process without setting ProcessStartInfo.WorkingDirectory to the folder containing exe and configuration and the started process shares the working directory with parent one, so either move appsettings.json to the parent project folder or set the WorkingDirectory to match the directory containing the exe:

toDoTest.StartInfo.UseShellExecute = false;
toDoTest.StartInfo.WorkingDirectory = "C:\\Develop\\ToDoMVCtutorial\\bin\\Release\\net6.0\\publish\\";

Also you can try redirecting your Web API output to capture the logs.



Related Topics



Leave a reply



Submit