模拟:ASP.Net MVC控制器操作与Web窗体 [英] Impersonation: ASP.Net MVC Controller Action vs. Web Forms

查看:73
本文介绍了模拟:ASP.Net MVC控制器操作与Web窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a difference with impersonation between an ASP.Net MVC controller actions vs. an ASP.Net Web Form? Using the exact same code within the same web project, I am able to successfully impersonate the Windows user when connecting to SQL Server from a Web Form but not from the Controller Action. Here is the code sample I am testing from each:

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表单以用户身份运行.对于这两个原因为何有所不同以及如何在控制器动作中模拟的任何澄清,将不胜感激.

I have checked the HttpContext user on both the controller and the web form and they look identical. However, when running a SQL trace the controller action is always running as Network Service, while the web form is running as the user. Any clarification on why these two are behaving different and how to impersonate within the controller action would be appreciated.

推荐答案

尝试添加

 <identity impersonate="true">

<system.web>

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

part of your web.config file for mvc app

这篇关于模拟:ASP.Net MVC控制器操作与Web窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆