来自.DSN文件的Entity Framework连接字符串

时间:2017-09-05 00:41:45

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

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

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

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

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

我可以找出读取.DSN文件的代码,所以如果你想提供一些代码示例,你可以根据它设置EF的连接字符串。

我也对其他解决方案持开放态度,甚至是我不应该这样做的原因。

提前致谢。

PS。在我写这篇文章时,我提出了一个小概念。我现在要测试一下,看看它是怎么回事。但这是基础知识:

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

  3. 3

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

    对此的想法可能?

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

1 个答案:

答案 0 :(得分:0)

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

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

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

请记住,我从未使用过.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;

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

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

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


}

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

再次告诉我,如果您喜欢或不喜欢这个解决方案,如果您不喜欢它,那么您的解决方案是什么以及为什么。

再次感谢!