简体   繁体   English

如何在运行时使用ASP.NET Core中的密码获取连接字符串

[英]How to get connection string in run-time with password in ASP.NET Core

Here i am using appsettings.json file for connection string. 在这里,我使用appsettings.json文件作为连接字符串。 I got connection string in runtime without password. 我在运行时没有密码就得到了连接字符串。

Example: 例:

((MySql.Data.MySqlClient.MySqlConnection)DataContext.Database.GetDbConnection()).ConnectionString

Please help to get password in runtime. 请帮助在运行时获取密码。

Thank in advance. 预先感谢。

You should try to get the connection string from Configuration, not from a DbConnection . 您应该尝试从Configuration而不是DbConnection获取连接字符串。 For example, if appsettings.json contains the following, you can get the connection string via Configuration.GetConnectionString("mydb") . 例如,如果appsettings.json包含以下内容,则可以通过Configuration.GetConnectionString("mydb")获得连接字符串。

{
  "ConnectionStrings": {
    "mydb": "server=DBSERVER;user=myuser;password=pwd;database=DB"
  }
}

That said, if you do have a DbConnection (or MySqlConnection ) object, you can get the password from the MySqlConnection.ConnectionString property if one or both of the following are true: 就是说,如果您确实具有DbConnection (或MySqlConnection )对象,则如果以下一项或两项为真,则可以从MySqlConnection.ConnectionString属性获取密码:

If you're able to modify the original connection string your application is using, you can add the PersistSecurityInfo=true flag. 如果您能够修改应用程序使用的原始连接字符串,则可以添加PersistSecurityInfo=true标志。 Otherwise, you will need to get an unopened database connection in order to retrieve the full connection string, including the password. 否则,您将需要获得未打开的数据库连接才能检索完整的连接字符串,包括密码。

This is by design, for security reasons. 出于安全原因,这是设计使然。 From MSDN : MSDN

The ConnectionString is similar to an OLE DB connection string, but is not identical. ConnectionString与OLE DB连接字符串相似,但不相同。 Unlike OLE DB or ADO, the connection string that is returned is the same as the user-set ConnectionString, minus security information if the Persist Security Info value is set to false (default). 与OLE DB或ADO不同,返回的连接字符串与用户设置的ConnectionString相同,如果Persist Security Info值设置为false(默认值),则减去安全信息。 The .NET Framework Data Provider for SQL Server does not persist or return the password in a connection string unless you set Persist Security Info to true. 除非将Persist Security Info设置为true,否则SQL Server的.NET Framework数据提供程序不会持续存在或在连接字符串中返回密码。

So, try to add Persist Security Info=true; 因此,尝试添加Persist Security Info=true; in your connection string. 在您的连接字符串中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在运行时重新加载自定义属性? ASP.NET核心MVC - How to reload a Custom Attribute during run-time? ASP.NET Core MVC 如何配置 Kestrel 以使用随机动态端口并在运行时使用 ASP.NET Core 3.1 确定端口? - How do you configure Kestrel to use a random dynamic port and determine the port at run-time with ASP.NET Core 3.1? ASP.Net Core 2.2 - 方法重载出现在 Visual Studio 中,但在运行时不起作用 - ASP.Net Core 2.2 - Method overload appears in Visual Studio but does not work at run-time 热门在运行时更改ASP.NET Core Angular应用程序中的主题 - Hot to change theme at run-time in ASP.NET Core Angular application Asp.Net Core 2.2由当前用户更改数据库运行时 - Asp.Net Core 2.2 change database run-time by current user 将行添加到HTML <Select>在ASP.Net的运行时 - Add row to HTML <Select> at Run-time in ASP.Net 如何使用 Dockerfile 覆盖 ASP.NET Core 连接字符串 - How to override ASP.NET Core connection string with Dockerfile 如何使用 ASP.NET Core DI 注入连接字符串 - How to inject connection string using ASP.NET Core DI 如何为我的 asp.net 核心 mvc 编写连接字符串 - How to write connection string for my asp.net core mvc 如何从ASP.Net Core中的SqlServerDBContextOptionsExtensions获取连接字符串 - How do I get the connection string from the SqlServerDBContextOptionsExtensions in ASP.Net Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM