2016-06-28 36 views
0

我是C#和MVC的新手。試圖創建一個允許用戶配置或更改初始SQL Server連接字符串的Intranet應用程序。該連接需要支持SQL混合模式身份驗證。雖然我的後端技能非常出色,但這是我第一次嘗試創建基於Web的Intranet Web應用程序。我一直在谷歌搜索3天,試圖找到一個沒有運氣的例子,教程或文檔。我已經達到了我不確定這是否被接受的做法。我正在使用VS2015,SQL Server 2012,C#,MVC,ASP.Net並以.Net Framework 4.61爲目標。任何指導表示讚賞。如何使用MVC/C創建最終用戶SQL Server連接對話框#

+1

'SqlConnectionStringBuilder'可能是您想要構建用戶定義的連接。一個簡單的表單將收集這些值,如果您正在收集姓名,地址,電話或服務器,數據庫,登錄 - 它的正確數據並不重要。 – Crowcoder

回答

1

也許你不能輕易改變默認/初始連接字符串中web.config動態(和認爲是有害的,直接改就web.config),但是SqlConnectionStringBuilder可以在運行時請求建立連接字符串:

// assume these properties are part of DB provider definition 
public class DatabaseSettings 
{ 
    public String ConnectionName { get; set; } 
    public String ServerName { get; set; } 
    public String DatabaseName { get; set; } 
    public String UserId { get; set; } 
    public String Password { get; set; } 
} 

// controller method definition 
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); 

builder.DataSource = DatabaseSettings.ServerName; 
builder.InitialCatalog = DatabaseSettings.DatabaseName; 
builder.IntegratedSecurity = true; 
builder.UserID = DatabaseSettings.UserId; 
builder.Password = DatabaseSettings.Password; 
builder.MultipleActiveResultSets = true; 

// modify initial connection string in runtime 
// note that ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString is readonly by default, 
// thus use reflection to disable private bReadOnly field before adding custom connection string 
var connectionString = ConfigurationManager.ConnectionStrings; 
var collection = typeof(ConfigurationElementCollection).GetField("bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic); 
collection.SetValue(connectionString, false); 

// This one may work if you tend to change default connection string value 
// connectionString[0].ConnectionString = builder.ToString(); 

// Safer way by adding new name rather than replace default one 
connectionString.Add(new ConnectionStringSettings(DatabaseSettings.ConnectionName, builder.ToString())); 

AFAIK您可以將用戶定義的連接字符串存儲在每個用戶的XML文件中,並在需要時編程加載(請參閱Reading connection string from external config file)。

關於混合模式(Windows爲內部網&互聯網& Forms身份驗證),設置你的IIS以2個入口點(即虛擬目錄),其中完整的應用程序應該依靠Forms身份驗證模式。 Windows身份驗證控制器通過傳遞用戶身份簡單地重定向到主站點。

在您的主站點登錄頁面上,您可能希望添加「使用Windows/Active Directory帳戶登錄」按鈕(類似於具有現有社交媒體帳戶的登錄按鈕)並單擊該按鈕將重定向Intranet用戶到專門製作爲從Windows身份驗證模式接收憑據。

也許這不是最好的方式來回答你的需求,但至少可以打開你的頭腦,學習和指出什麼事情要做。

參考文獻:

1)改變連接字符串編程

http://david.gardiner.net.au/2008/09/programmatically-setting.html

2)雙模認證

https://msdn.microsoft.com/en-us/library/bb669066(v=vs.110).aspx(MS SQL服務器驗證物品)

http://www.codeguru.com/csharp/.net/net_security/authentication/article.php/c19559/ASPNET-Mixed-Mode-Authentication.htm

http://mvolo.com/iis-70-twolevel-authentication-with-forms-authentication-and-windows-authentication/

+0

這正是我所需要的。謝謝你們的幫助! –

相關問題