ASP.NET MVC IIS 7 - Request.ServerVariables[“LOGON_USER”] 返回空字符串

发布于 2024-12-15 22:32:05 字数 598 浏览 3 评论 0原文

我的应用程序具有表单身份验证,但当用户是管理员时我会绕过表单。我通过使用以下代码提取用户名来完成此操作:

Request.ServerVariables["LOGON_USER"];

提取后,我使用返回的名称对 LDAP 进行身份验证。当在我的本地计算机上的 VS 中运行时,这工作得很好。

当我使用 IIS 运行时,该值为空。原因是我的应用程序启用了匿名身份验证。参考:http://support.microsoft.com/kb/306359

据我所知,我需要为具有表单身份验证的应用程序启用匿名身份验证。

无论如何我可以拉出登录电脑的用户吗?

更新:我得出的结论是我不能使用匿名身份验证。当我只有表单身份验证设置时,出现以下错误:

HTTP Error 401.2 - Unauthorized
You are not authorized to view this page due to invalid authentication headers.

My app has form authentication but I bypass the form when the user is an admin. I do this by pulling the username with this code:

Request.ServerVariables["LOGON_USER"];

Once pulled, I use the name returned to authenticate against LDAP. This works fine when run inside VS on my local machine.

When I run with IIS this value is blank. The reason for this is due to having anonymous authentication enabled for my app. Ref: http://support.microsoft.com/kb/306359

As far as I know I require Anonymous Authentication enabled for apps with forms authentication.

Is there anyway I can pull the user that is logged into the PC?

Update: I've come to the conclusion that I can't use Anonymous Authentication. I get the following error when I only have the Forms Authentication setup though:

HTTP Error 401.2 - Unauthorized
You are not authorized to view this page due to invalid authentication headers.

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

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

发布评论

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

评论(3

近箐 2024-12-22 22:32:05

最后一个答案是微软的解决方案,但我找到了真正的原因:

显然,ServerVariables已经为C#贬值了

所以对于C#,你需要这样做:

string login = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

我不想删除最后一个答案,因为它可能适用于运行旧版本 C# 的人。

Last answer is Microsoft's solution, but I found the real reason:

Apparently, ServerVariables have been depreciated for C#

So for C#, you need to do it this way:

string login = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

I don't want to delete the last answer, because it may work for people running on older versions of C#.

眉目亦如画i 2024-12-22 22:32:05

您可以混合使用表单和 Windows 身份验证。在这里查看类似的解决方案: http://www.pluralsight-training.net/community/blogs/craig/archive/2004/07/24/1699.aspx

您可能没有登录表单中描述的复选框,但您可以只检查 HTTP 标头中的 LOGON_USER。

You can mix forms and windows authentication. Have a look at a similar solution here: http://www.pluralsight-training.net/community/blogs/craig/archive/2004/07/24/1699.aspx

You might not have the described checkbox in the login form, but you could just check the LOGON_USER in the HTTP header.

末骤雨初歇 2024-12-22 22:32:05

知识库文章PRB:Request.ServerVariables("LOGON_USER") 在 ASP.NET 中返回空字符串从不表示 所在位置。

<system.web>
  <authentication mode="Windows" />
</system.web>

显然,我也有这个问题,否则我不会在这里。

The KB article PRB: Request.ServerVariables("LOGON_USER") Returns Empty String in ASP.NET never says where <authentication mode="Windows" /> goes.

<system.web>
  <authentication mode="Windows" />
</system.web>

Obviously, I am having issues with this too, or I would not be here.

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