实体框架连接字符串从.DSN文件

问题描述:

我有一个问题,所以我想我会来网上最聪明的头脑。实体框架连接字符串从.DSN文件

我写了一个ASP.NET MVC应用程序,它与另一个应用程序提供的Web服务接口。我的应用程序基本上只是增加了一些功能到其他Web应用程序

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

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

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

我也开放给其他解决方案,甚至为什么我不应该这样做。

在此先感谢。

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

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

3.

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

思考,也许?

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

+0

我怀疑EF是否支持SQL Server DSN,为什么不试图直接提供服务器凭证?你有没有使用DB First与其他服务器实例连接? –

+0

是的,EF不支持DSN。我不想直接应用凭据的原因是我不想存储它们。我只想使用其他应用程序使用的相同用户。 – JohZant

+1

听起来像你希望用户使用每个凭证(即动态生成的连接字符串)访问不同的数据库,是吗?我想你可以将DB连接字符串传递给实体局部类的构造函数。 –

所以,我真的很具有最大的问题是如何设置的实体框架我的连接字符串。但我也希望也许有人曾经使用.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; 

现在,我有连接字符串保存的,我只是有我的数据库环境中使用它下面,

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

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


} 

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

再次,让我知道如果你喜欢或不喜欢这个解决方案,如果你不喜欢它,你的解决方案是什么,为什么。

再次感谢!