简体   繁体   English

关于Windows模拟的建议,但更改为其他用户

[英]Advice on Windows Impersonation but changing to another user

I have the following scenario where I'm planning in using windows authentication. 我在以下情况下计划使用Windows身份验证。

1.1) I have a web server which will run within a domain. 1.1)我有一个将在域中运行的Web服务器。
1.2) The web site will run under the credentials of a domain user with a set of configured permissions (One which will be allowed access to the file system, SQL server database etc). 1.2)该网站将在具有一组已配置权限的域用户的凭据下运行(将被允许访问文件系统,SQL Server数据库等)。
1.3) Users visiting the web site will belong to the same domain, so I'm planning in using windows authentication. 1.3)访问该网站的用户将属于同一域,因此我打算使用Windows身份验证。

So at this point, an authenticated user, would access the site, but I guess that from code, “CurrentUser” would be the one under which the site is running. 因此,在这一点上,经过身份验证的用户可以访问该站点,但是我想从代码中可以看出,“ CurrentUser”将是运行该站点的用户。

I'd like the following. 我想要以下。

2.1) To authenticate the user accessing the site with windows authentication. 2.1)使用Windows身份验证来验证访问站点的用户。 (Domain controller would be responsible for this). (域控制器将对此负责)。
2.2) For the site to run under the configured user from step 1.2. 2.2)使站点在步骤1.2中配置的用户下运行。 So it would have all of its permissions. 因此它将拥有所有权限。
2.3) But I'd like to know the initial user used to authenticate from (step 2.1). 2.3)但我想知道最初用来进行身份验证的用户(步骤2.1)。

This way I could do the following: 这样,我可以执行以下操作:

3.1) User “A” decides to access the site, as he belongs to the same domain as the web server, he authenticates successfully. 3.1)用户“ A”决定访问该站点,因为他与Web服务器属于同一域,因此他成功进行了身份验证。
3.2) From code I detect that “A” authenticated, so I'll go and fetch his roles. 3.2)从代码中,我检测到“ A”已通过身份验证,因此我将去获取他的角色。 “Role1, Role2, Role3”. “角色1,角色2,角色3”。
3.3) I then want the code to run under the user configured in step 1.2, but I'll assign the Principal all of the roles retrieved from 3.2. 3.3)然后,我希望代码在步骤1.2中配置的用户下运行,但是我将为Principal分配从3.2检索到的所有角色。

I've thought that maybe I could use Impersonation for this. 我想也许我可以为此使用模拟。

4.1) So user “A” decides to access the site and authenticates. 4.1)因此,用户“ A”决定访问该站点并进行身份验证。
4.2) The site would initially run with “A” credentials, so the “CurrentUser” would be “A”. 4.2)该站点最初将使用“ A”凭据运行,因此“ CurrentUser”将为“ A”。
4.3) Switch the user (somehow) back to the one from 1.2 4.3)将用户(以某种方式)从1.2切换回原来的状态
4.4) I could retrieve all of 4.1 configured Roles. 4.4)我可以检索所有4.1配置的角色。
4.5) Assign the Current Principal the roles retrieved from 4.4. 4.5)为当前负责人分配从4.4中检索到的角色。

So in the end the web site will use Windows Authentication with Impersonation, but from code I'd switch back to user 1.2. 因此,最终该网站将使用Windows身份验证身份验证,但是从代码中我将切换回用户1.2。

If you've reached this point thanks for reading! 如果您已经达到了这一点,则感谢您的阅读! I'd like to know if this is possible and if it seems achievable or if I'm overcomplicating things. 我想知道这是否可行,似乎可以实现,还是我使事情变得过于复杂。

Also suggestions in how can I plug into and where to do all the role retrieving and user switching. 还提供了有关如何插入角色以及在哪里执行所有角色检索和用户切换的建议。
Many thanks! 非常感谢!

UPDATE 1 更新1

@ Code Jammr , you're right, no need to do any crazy stuff. @ Code Jammr,您是对的,不需要做任何疯狂的事情。 But I think I still need to look into HttpModules,.. 但是我认为我仍然需要研究HttpModules。

After doing a few tests, searching etc... 经过几次测试后,进行搜索等...

I've started to understand the difference between these IIdentity objects: 我已经开始理解这些IIdentity对象之间的区别:

HttpContext.Current.User.Identity HttpContext.Current.User.Identity
Thread.CurrentPrincipal.Identity Thread.CurrentPrincipal.Identity
WindowsIdentity i2 = WindowsIdentity.GetCurrent(); WindowsIdentity i2 = WindowsIdentity.GetCurrent();

I posted another question to help me understand them: Help understanding impersonation 我发布了另一个问题来帮助我理解它们: 帮助理解模仿

I think this answers my question. 我认为这回答了我的问题。

There are several code samples out there for doing Impersonation. 有一些代码示例可以进行模拟。 Most involve dealing with tokens and Win API calls. 大多数涉及处理令牌和Win API调用。 But if you really must do it this way, I say this not knowing what your webserver type is. 但是,如果您真的必须这样做,我就说这不知道您的Web服务器类型是什么。 IIS 6 or IIS7, then there are many code samples out there to guide you along. IIS 6或IIS7,那么这里有许多代码示例可以指导您进行操作。

Here is one link for ya that pretty much gives you a starting point. 这是ya的一个链接,它几乎为您提供了一个起点。 http://msdn.microsoft.com/en-us/library/aa331755(v=vs.71).aspx http://msdn.microsoft.com/zh-CN/library/aa331755(v=vs.71).aspx

Here is a link on AD authentication and you may not have to do anything crazy. 这是有关AD身份验证的链接,您可能不必做任何疯狂的事情。 http://support.microsoft.com/kb/326340 http://support.microsoft.com/kb/326340

You may want to look into asp.net impersonation, app pool settings, etc... to see if there is a better way. 您可能需要研究asp.net的模拟,应用程序池设置等,以查看是否有更好的方法。

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

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