ASP.NET 模拟问题(第 2 部分)

发布于 2024-11-08 13:10:56 字数 1275 浏览 9 评论 0原文

这是关于无法模拟当前登录的 Windows 用户的上一篇文章的后续内容。有很多好的建议,但之前的帖子变得混乱,所以我用这篇文章重新设置。希望根据下面记录的当前状态,问题是什么会很明显。这是一条很旧的路径,所以我不得不相信我所缺少的只是一个小小的配置步骤。

问题:我需要让 ASP.NET 模拟当前登录的用户。当我在 IIS 7.5 下运行时,它不起作用。 IIS Express 工作正常,但我相信这是因为调试会话在我的用户 ID 下运行。

我使用 Environment.Username 来确定该用户是谁。有人建议该属性始终返回登录的用户名,但根据我的测试,它返回来自 IIS 的模拟用户。

例如,如果我的 web.config 有...

    <identity impersonate="true" />

当我使用该设置在 IIS 7.5 下运行时,Environment.Username 返回 IUSR。我相信这是 IIS 匿名用户帐户。

如果我将 web.config 更改为……

    <identity impersonate="true" userName="domain\jlivermore" password="mypassword" />

,则 Environment.Username 返回 jlivemore。但是,我需要它返回 jlivermore,而无需在 web.config 中明确设置它。

这是我的 IIS 设置...

.NET 授权规则 .NET 授权规则

身份验证 Authentication

有一个问题,如果我禁用匿名身份验证,系统会提示我登录该网站。我想如果您使用域上的 Active Directory 帐户登录,那么这个挑战就不会出现?即使我在这个提示中输入我的用户名/密码,我仍然无法让模拟工作。

在此处输入图像描述

基本设置

基本设置

This is a follow on to a previous post about being unable to impersonate a currently logged in Windows user. There were many good suggestions, but the previous thread was getting messy, so I am resetting with this post. Hopefully with the current state documented below it will be obvious what the issue is. This is a well worn path, so I have to believe all I am missing is a little configuration step.

PROBLEM: I need to have ASP.NET impersonate the currently logged in user. When I run under IIS 7.5, it doesn't work. IIS Express works fine, but I believe that is because the debugging session is running under my user id.

I am using Environment.Username to determine who this user is. There was a suggestion that this property always returns the logged in user name, but from my testing it returns the impersonated user from IIS.

For example, if my web.config has…

    <identity impersonate="true" />

When I run under IIS 7.5 with that setting, Environment.Username returns IUSR. I believe this is the IIS anonymous user account.

If I change web.config to…

    <identity impersonate="true" userName="domain\jlivermore" password="mypassword" />

… then Environment.Username returns jlivemore. However, I need it to return jlivermore without me explicitly setting it in web.config.

Here are my IIS settings…

.NET Authorization Rules
.NET Authorization Rules

Authentication
Authentication

One question, if I disable Anonymous Authentication, then I am prompted to login to the site. I thought if you were logged in with an Active Directory account on a domain then this challenge wouldn't appear? Even if I enter my username/password into this prompt, I still don't get the impersonation to work.

enter image description here

Basic Settings

Basic Settings

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魔法唧唧 2024-11-15 13:10:56

我不确定您是否找到了答案,但如果有人遇到问题,您将需要在 web.config 文件中添加以下内容

<authentication mode="Windows"/>
<identity impersonate="true"/>

,并且在 IIS 中,您将需要启用 Asp.net 模拟并启用 Windows 身份验证,其他的应该被禁用。在 Windows 身份验证中,转到高级设置并取消选中启用内核模式身份验证。应该可以做到这一点。您的站点现在应该设置为本地 Intranet 应用程序,并且使用以下任何一项都可以工作

System.Security.Principal.WindowsIdentity.GetCurrent().Username()
HttpContext.Current.User.Identity.Name
System.Threading.Thread.CurrentPrincipal.Identity.Name

,但是使用 Environment.Username 只会返回服务器名称,希望这可以帮助任何遇到此问题的人

I'm not sure if you've found an answer, but if anyone is having problems with it you will need the following in your web.config file

<authentication mode="Windows"/>
<identity impersonate="true"/>

And in IIS you will need Asp.net Impersonation enabled as well as Windows Authentication enabled, the others should be disabled. And in Windows Authentication, go to Advanced Settings and UNCHECK the Enable Kernel-mode authentication. That should do it. Your site should now be set for Local Intranet apps and using any of the following will work

System.Security.Principal.WindowsIdentity.GetCurrent().Username()
HttpContext.Current.User.Identity.Name
System.Threading.Thread.CurrentPrincipal.Identity.Name

But using Environment.Username will only return the server name, hopefully this helps anyone struggling with this

醉殇 2024-11-15 13:10:56

我遇到了与你描述的类似的问题。问题的基本症结在于模拟和委托之间存在差异。我对此的简单理解是,当客户端和服务器位于同一台机器上时,模拟就会起作用。但是,如果客户端位于不同的计算机上,则需要委派。

MSDN 参考

模拟和委托有什么区别?

模仿原始流
呼叫者身份到后端
同一台计算机上的资源。
委托流向原始调用者的
后端资源的身份
电脑以外的电脑
运行服务。

相关 SO 问题

  • ASP.NET MVC 中的模拟< /一>
  • <一href="https://stackoverflow.com/questions/5242210/starting-a-console-application-from-asp-net-using-authenticated-user-credentials">使用经过身份验证的用户凭据从 asp.net 启动控制台应用程序

I had a similar problem as you describe. The basic crux of the matter is that there is a difference between impersonation and delegation. My simple understanding of this is that impersonation will work when the client and server are on the same machine. If however, the client is on a different machine, you need delegation.

MSDN Reference

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.

Related SO questions

梦断已成空 2024-11-15 13:10:56

你有没有尝试过使用

HttpContext.Current.User.Identity.Name ?

Have you tried using

HttpContext.Current.User.Identity.Name ?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文