模拟:ASP.Net MVC控制器操作与Web表单

Saa*_*per 5 asp.net asp.net-mvc impersonation webforms sqlconnection

ASP.Net MVC控制器操作与ASP.Net Web窗体之间的模拟是否存在差异?在同一个Web项目中使用完全相同的代码,我可以在从Web窗体连接到SQL Server而不是从Controller Action连接到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);
Run Code Online (Sandbox Code Playgroud)

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

Ale*_*ran 5

尝试添加

 <identity impersonate="true">
Run Code Online (Sandbox Code Playgroud)

<system.web>
Run Code Online (Sandbox Code Playgroud)

mvc app的web.config文件的一部分


Kei*_*ler 1

这可能会有所帮助:

ASP.NET MVC 中的模拟

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

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