简体   繁体   English

ASP.NET MVC中的模拟

[英]Impersonation in ASP.NET MVC

I have a MVC web application on an intranet and want to be able to create files on our FTP server to send to outside partners. 我在Intranet上有一个MVC Web应用程序,希望能够在我们的FTP服务器上创建文件以发送给外部合作伙伴。

The code for impersonation uses the WindowsImpersonationContext. 模拟代码使用WindowsImpersonationContext。

System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();

StreamWriter sw = System.IO.File.CreateText("PathOnFTPServer");
sw.Write("data");

impersonationContext.Undo();

Here's what's happening and the reason for my question: 这是发生了什么以及我的问题的原因:

Pre Impersonation 预先假冒

User.Identity.Name: [my windows credentials] User.Identity.Name:[我的Windows凭据]

System.Security.Principal.WindowsIdentity.GetCurrent().Name: NT AUTHORITY\\NETWORK SERVICE System.Security.Principal.WindowsIdentity.GetCurrent()。名称:NT AUTHORITY \\ NETWORK SERVICE

Post Impersonation 发布模仿

User.Identity: [my windows credentials] User.Identity:[我的Windows凭据]

GetCurrent.Name: [my windows credentials] GetCurrent.Name:[我的窗口凭据]

Impersonate Undo 模仿撤消

User.Identity: [my windows credentials] User.Identity:[我的Windows凭据]

GetCurrent.Name: NT AUTHORITY\\NETWORK SERVICE GetCurrent.Name:NT AUTHORITY \\ NETWORK SERVICE

So, before I impersonate, the current user is the System Account but after impersonation, it is using my windows domain account which has permission to create text files on the FTP server. 因此,在我冒充之前,当前用户是系统帐户,但在模仿之后,它正在使用我的Windows域帐户,该帐户有权在FTP服务器上创建文本文件。 The code works locally using the visual studio web server but not when I deploy it on IIS on our test server. 该代码使用visual studio Web服务器在本地工作,但在我在测试服务器上的IIS上部署时则不行。

I'm getting an access denied error. 我收到拒绝访问错误。 What would be the reason for the error when the correct user is being impersonated? 正确的用户被模拟时出现错误的原因是什么?

Impersonation allows machine to machine impersonation, so the client browser and the server are on the same page when it comes to the impersonation. 模拟允许机器进行模拟,因此客户端浏览器和服务器在模拟时位于同一页面上。 When you then attempt to access the network share, the computer doesn't trust the impersonated credentials. 然后,当您尝试访问网络共享时,计算机不信任模拟的凭据。

You need to enable delegation for the IIS machine in Active Directory. 您需要在Active Directory中为IIS计算机启用委派。 Go to Active Directory Users and Computers, find the computer, click properties, and 'Trust computer for delegation'. 转到“Active Directory用户和计算机”,找到计算机,单击“属性”和“信任计算机以进行委派”。 (You might need to restart IIS for this to work, I don't remember). (您可能需要重新启动IIS才能使用此功能,我不记得了)。

There is way more theory than this that I don't fully understand, but this should work. 有更多的理论比我不完全理解,但这应该有效。 Whether it is right or not someone else could comment on! 是否对别人可以发表评论!

Also, the reason it works on your development machine is that the development server runs as the developer, not (Local)\\Network Service. 此外,它在您的开发机器上工作的原因是开发服务器作为开发人员运行,而不是(本地)\\网络服务。


A decent link: 一个不错的链接:

http://msdn.microsoft.com/en-us/library/cc949004.aspx http://msdn.microsoft.com/en-us/library/cc949004.aspx

What is the difference between impersonation and delegation? 模仿和授权有什么区别?

Impersonation flows the original caller's identity to back-end resources on the same computer. 模拟将原始调用方的标识传递到同一台计算机上的后端资源。 Delegation flows the original caller's identity to back-end resources on computers other than the computer running the service. 委派将原始呼叫者的身份传递给运行该服务的计算机以外的计算机上的后端资源。

For example, if a service is running within IIS without impersonation, the service will access resources using the ASP.NET account in IIS 5.0, or the Network Service account in IIS 6.0. 例如,如果服务在IIS中运行而不进行模拟,则该服务将使用IIS 5.0中的ASP.NET帐户或IIS 6.0中的网络服务帐户访问资源。 With impersonation, if the client is connecting using the original caller's account, the service will access resources such as a SQL Server database on the same machine using the original caller's account instead of the system ASP.NET account. 通过模拟,如果客户端使用原始调用方的帐户进行连接,则该服务将使用原始调用方的帐户而不是系统ASP.NET帐户访问同一台计算机上的SQL Server数据库等资源。 Delegation is similar except that the SQL Server database could be on a different machine that is remote to the service. 委派类似,只是SQL Server数据库可能位于远离服务的其他计算机上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM