【问题标题】:Entity Framework connection string from .DSN file.DSN 文件中的实体框架连接字符串
【发布时间】:2017-09-07 12:23:57
【问题描述】:

我有一个问题,所以我想我会找到网络上最聪明的人。

我编写了一个 ASP.NET MVC 应用程序,它与另一个应用程序提供的 Web 服务交互。我的应用程序基本上只是为其他 Web 应用程序添加了一些功能。

两个应用程序都有一个数据库。我试图通过使用其他应用程序 SQL Server 凭据来限制我的应用程序的配置。这样一来,如果他们决定更改其他应用程序的密码,我的就可以开始工作了。

这些凭据保存在我的应用程序可以访问的 .DSN 文件中。如何让使用 Entity Framework 的应用程序使用从 .DSN 文件中读取的详细信息创建的连接字符串?

我可以找出读取 .DSN 文件的代码,因此,如果您希望提供一些代码示例,您可以围绕设置 EF 的连接字符串为基础。

我也愿意接受其他解决方案,甚至是我不应该这样做的原因。

提前致谢。

PS。在写这篇文章的时候,我想出了一个小概念。我现在要测试它,看看它是如何进行的。但这里是基础:

  1. 在启动时,将所需的详细信息读入静态属性。
  2. public MyContext() : base(getConnectionString()) { }

3.

private SomeObjectTypeHere getConnectionString()
{
   //read static properties
   //return .....something..... not sure yet....
}

可能有这样的想法?

编辑 我创建了一个读取 .DSN 文件并获取服务器、用户 ID 和密码的方法。我现在将这些存储在静态属性中。在我的上下文中,既然我有所需的详细信息,我该如何设置我的连接字符串。

【问题讨论】:

  • 我怀疑 EF 是否支持 SQL Server DSN,为什么不尝试直接提供服务器凭据呢?您是否使用 DB First 连接其他服务器实例?
  • 是的,EF 不支持 DSN。我不想直接应用凭据的原因是我不想存储它们。我只想使用其他应用程序使用的同一用户。
  • 听起来您希望用户使用每个凭据(即动态生成的连接字符串)访问不同的数据库,对吗?我认为您可以将数据库连接字符串传递给实体部分类的构造函数。
  • 对不起,我误读了你的大部分帖子。所以真的,我想你只需要使用SqlConnectionStringBuilder 将各个部分放在一起
  • 正如我之前提到的,您可以创建SqlConnectionStringBuilder 的新实例,例如var connectionString = new SqlConnectionStringBuilder() { ... }。然后将生成的连接字符串传递给 EF 实体的构造函数(在此处提供创建用于存储凭据的方法)。

标签: c# asp.net asp.net-mvc entity-framework dsn


【解决方案1】:

所以,我真正遇到的最大问题是如何在 Entity Framework 中设置我的连接字符串。但我也希望也许其他人处理过 .DSN 文件。

无论如何,这是我的解决方案。仍在寻找可能由此产生的问题,所以如果您发现任何问题,请告诉我!

首先,我创建了一个在启动时运行的方法。此方法遍历 .DSN 文件并挑选出 gem。

请记住,我从未使用过 .DSN 文件,获取密码的部分因我的情况而异。

            var DSNFileContents = File.ReadAllLines(WebConfigurationManager.AppSettings["AppPath"] + @"\App.DSN");//reads DSN into a string array

            //get UID
            string uid = DSNFileContents.Where(line => line.StartsWith("UID")).First().Substring(4);//get UID from array
            //test if uid has quotes around it
            if (uid[0] == '"' && uid[uid.Length - 1] == '"')
            {
                //if to starts with a quote AND ends with a quote, remove the quotes at both ends
                uid = uid.Substring(1, uid.Length - 2);
            }

            //get server
            string server = DSNFileContents.Where(line => line.StartsWith("SERVER")).First().Substring(7);//get the server from the array
            //test if server has quotes around it
            if (server[0] == '"' && server[server.Length - 1] == '"')
            {
                //if to starts with a quote AND ends with a quote, remove the quotes at both ends
                server = server.Substring(1, server.Length - 2);
            }

            //THIS WON'T WORK 100% FOR ANYONE ELSE. WILL NEED TO BE ADAPTED
            //test if PWD is encoded
            string password = "";
            if (DSNFileContents.Where(line => line.StartsWith("PWD")).First().StartsWith("PWD=/Crypto:"))
            {
                string secretkey = "<secret>";
                string IV = "<alsoSecret>";
                byte[] encoded = Convert.FromBase64String(DSNFileContents.Where(line => line.StartsWith("PWD")).First().Substring(12));
                //THIS LINE IN PARTICULAR WILL NOT WORK AS DecodeSQLPassword is a private method I wrote to break the other applications encryption
                password = DecodeSQLPassword(encoded, secretkey, IV);
            }
            else
            {
                //password was not encrypted
                password = DSNFileContents.Where(line => line.StartsWith("PWD")).First().Substring(4);
            }

            //build connection string
            SqlConnectionStringBuilder cString = new SqlConnectionStringBuilder();
            cString.UserID = uid;
            cString.Password = password;
            cString.InitialCatalog = "mydatabase";
            cString.DataSource = server;
            cString.ConnectTimeout = 30;
            //statProps is a static class that I have created to hold some variables that are used globally so that I don't have to I/O too much.
            statProps.ConnectionString = cString.ConnectionString;

现在我已经保存了连接字符串,我只是让我的数据库上下文使用它,如下所示,

public class myContext : DbContext
{
    public myContext() : base(statProps.ConnectionString) { }

    //all my DbSets e.g.
    public DbSet<Person> Persons{ get; set; }


}

这很简单,是的,但我希望它可以为任何想要做类似事情但不确定应该如何处理的人提供一些信息。

同样,如果您喜欢或不喜欢此解决方案,请告诉我,如果您不喜欢,您的解决方案是什么以及原因。

再次感谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 2012-10-11
    相关资源
    最近更新 更多