ASP.Net MVC Controller 操作与ASP.Net Web窗体之间的模拟是否有区别?在同一Web项目中使用完全相同的代码,当从Web窗体而不是从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);
我已经检查了 Controller 和Web窗体上的HttpContext用户,它们看起来相同。但是,在运行SQL跟踪时, Controller 操作始终作为网络服务运行,而Web表单以用户身份运行。关于这两个为什么表现不同以及如何在 Controller Action 中模拟的任何澄清都将受到赞赏。
最佳答案
尝试添加
<identity impersonate="true">
至
<system.web>
适用于mvc应用的web.config文件的一部分
关于asp.net - 模拟:ASP.Net MVC Controller 操作与Web窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1909114/