5

ASP.Net MVC 控制器操作与 ASP.Net Web 表单之间的模拟有区别吗?在同一个 Web 项目中使用完全相同的代码,当从 Web 窗体连接到 SQL Server 时,我能够成功地模拟 Windows 用户,但不能从控制器操作。这是我正在测试的代码示例:

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);

我已经检查了控制器和 Web 表单上的 HttpContext 用户,它们看起来相同。但是,在运行 SQL 跟踪时,控制器操作始终作为网络服务运行,而 Web 表单作为用户运行。任何关于为什么这两个行为不同以及如何在控制器操作中模拟的澄清将不胜感激。

4

2 回答 2

5

尝试添加

 <identity impersonate="true">

<system.web>

mvc 应用程序的 web.config 文件的一部分

于 2009-12-17T11:54:51.417 回答
1

这可能会有所帮助:

ASP.NET MVC 中的模拟

我还应该提到,模拟可能会对您扩展应用程序的能力产生负面影响:

http://www.hanselman.com/blog/AvoidUsingImpersonationInASPNET.aspx

于 2009-12-15T21:41:02.440 回答