2009-12-15 31 views
5

ASP.Net MVC控制器動作與ASP.Net Web窗體之間的模擬有區別嗎?在同一個Web項目中使用完全相同的代碼,我可以在從Web窗體連接到SQL Server時成功模擬Windows用戶,但不能從Controller Action中連接到SQL Server。下面是代碼示例我從每個測試:模仿:ASP.Net MVC控制器動作與Web窗體

string sqlQuery = @"SELECT Top 10 FullName FROM Customer"; 

// Connect to the database server. You must use Windows Authentication; 
SqlConnection connection = new SqlConnection("Data Source=ServerName;Initial Catalog=DBName;Integrated Security=SSPI"); 
// Create a DataTable to store the results of the query. 
DataTable table = new DataTable(); 

// Create and configure the SQL Data Adapter that will fill the DataTable. 
SqlDataAdapter adapter = new SqlDataAdapter(); 
adapter.SelectCommand = new SqlCommand(sqlQuery, connection); 

// Execute the query by filling the DataTable. 
adapter.Fill(table); 

我已經檢查的HttpContext用戶控制器和Web表單上都和他們看起來是相同的。但是,在運行SQL跟蹤時,控制器操作始終以網絡服務運行,而Web窗體以用戶身份運行。任何澄清爲什麼這兩個表現不同以及如何在控制器動作內模仿,將不勝感激。

回答

5

嘗試添加

<identity impersonate="true"> 

<system.web> 

爲MVC應用程序

+0

謝謝,這對我有用! –