简体   繁体   English

c#asp.net获取用户名

[英]c# asp.net getting username

"Have you ever done any .net programming? Yes? Good, here's a massive broken program, fix it". “你有没有做过任何.net编程?是的?好的,这是一个大规模的破解程序,修复它”。 That is the situation I'm in, so sorry if it's an easy question. 这就是我所处的情况,如果这是一个简单的问题,那就很抱歉。

The program I am working on pulls a file from a web server. 我正在处理的程序从Web服务器中提取文件。 It is expected that the user is already logged into the web server. 预计用户已登录到Web服务器。 I need to pull the username of the current person logged into the server (or just make sure someone is indeed logged into the server). 我需要提取登录到服务器的当前用户的用户名(或者只是确保某人确实已登录到服务器)。

I have tried the following and it returns an empty string. 我尝试了以下内容,它返回一个空字符串。

user = HttpContext.Current.User.Identity.Name;

Please make sure you are setting windows authentication in Web.Config file. 请确保您在Web.Config文件中设置Windows身份验证。 Also check the following before accessing the username, 在访问用户名之前,请检查以下内容,

HttpContext.Current.User.Identity.IsAuthenticated

Set Web.Config as follows, 设置Web.Config如下,

<authentication mode="Windows"></authentication>

First check in Web.config file for <authentication> tag. 首先在Web.config文件中检查<authentication>标记。 If you don't find it then your application may not be using any standard authentication mechanism. 如果您没有找到它,那么您的应用程序可能没有使用任何标准身份验证机制。 If that is the case look inside the login.aspx or whatever code that does the authentication. 如果是这种情况,请查看login.aspx或进行身份验证的任何代码。 There you will get hold of logged in user data. 在那里,您将获得登录的用户数据。

I wouldn't recommend you to change anything in web.config file without having some firm grasp on whats going in the application. 我不建议你在web.config文件中更改任何内容,而不必掌握应用程序中的最新信息。

The properties of the User object are usually populated by the application's authentication scheme (Forms, Windows or Custom) so you'll need to ensure one of these is in place before commencing to access the User . User对象的属性通常由应用程序的身份验证方案(Forms,Windows或Custom)填充,因此在开始访问User之前,您需要确保其中一个已到位。

For more information, take a look at the docs at MSDN . 有关更多信息,请查看MSDN上的文档

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

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