简体   繁体   English

如何获取当前Windows登录用户的用户名?

[英]How do I get the current Windows logged-in user's username?

I am developing an intranet application using ASP.NET MVC 4. I am using custom forms authentication. 我正在使用ASP.NET MVC 4开发Intranet应用程序。我正在使用自定义表单身份验证。 When the user accesses the application I want to take the user's Windows logged-in username and check that username in my database. 当用户访问该应用程序时,我要使用该用户的Windows登录用户名并在我的数据库中检查该用户名。 But I don't know how to take that username. 但是我不知道如何使用该用户名。 I try to take it using the following code: 我尝试使用以下代码:

string CurLoggedInUsername = Environment.UserName;

This gives me the username when I run from Visual Studio, but when I host my application on IIS it gives a weird value. 当我从Visual Studio运行时,这给了我用户名,但是当我在IIS上托管我的应用程序时,它给出了一个奇怪的值。 I also tried to use 我也尝试使用

string CurLoggedInUsername = httpcontext.current.user.identity.name;

but no use. 但没有用。 Is there a way to get the user's Windows logged-in username before authentication? 有没有办法在身份验证之前获取用户的Windows登录用户名?

It sounds like you may need to consider mixed-mode authentication. 听起来您可能需要考虑混合模式身份验证。 Ie Windows and Forms authentication. 即Windows和窗体身份验证。 The thing is they don't necessarily play well together. 问题是他们并不一定会打得很好。

The following article describes the problem and how to overcome it. 下面的文章介绍了该问题及其解决方法。 The thrust of the article centres around how a 401 challenge (Windows authentication) and 302 redirect (forms authentication) are incompatible in > IIS 7 integrated mode and a way to use a couple of forms and HTTP pipeline interception to get at user details. 本文的重点围绕IIS 7集成模式下401质询(Windows身份验证)和302重定向(表单身份验证)如何不兼容以及使用几种表单和HTTP管道拦截获取用户详细信息的方式。 I used the approach successfully for a large public sector client (albeit for a webforms application). 我成功地将这种方法用于大型公共部门客户(尽管适用于Webforms应用程序)。 I'm sure the principles are the same. 我确定原理是相同的。

IIS 7.0 Two-Level Authentication with Forms Authentication and Windows Authentication 带有表单身份验证和Windows身份验证的IIS 7.0两级身份验证

I forgot to add in the code examples the right place to look for Windows authentication credentials are presented. 我忘记在代码示例中添加显示Windows身份验证凭据的正确位置。 It's been a while, so I can't remember it off the top of my head. 已经有一段时间了,所以我记不起来了。

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

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