简体   繁体   English

asp.net中的用户名

[英]User name in asp.net

I tryed this ways:我试过这种方式:

Request.ServerVariables["LOGON_USER"]

or或者

HttpContext.Current.User.Identity.Name 

or或者

User.Identity.Name

-- If i run it by F5 from VS2010, it runs OK. -- 如果我从 VS2010 按 F5 运行它,它运行正常。

-- If i run it on IIS (I tryed it on 5.1 and 6.0, other IIS i can't use) there are empty strings. -- 如果我在 IIS 上运行它(我在 5.1 和 6.0 上尝试过,其他 IIS 我不能使用)有空字符串。

In web.config i have:在 web.config 我有:

<authentication mode="Windows"/>
<authorization>
  <allow users="*"/>
</authorization>

so, all users should by autentificated.所以,所有用户都应该通过认证。

Maybe, there should be more things in web.config.也许,web.config 中应该有更多的东西。

I tryed it in IE, Firefox, and Chrome.我在 IE、Firefox 和 Chrome 中尝试过。

I post this question before, but there was some misleading information, so i post it again.我之前发布过这个问题,但有一些误导性信息,所以我再次发布。

so, all users should by autentificated.所以,所有用户都应该通过认证。

Exactly.确切地。 All users include anonimous.所有用户都包括匿名用户。

Any web browser will attempt an anonymous request at first, if it's successful it won't try to authenticate.任何 web 浏览器都会首先尝试匿名请求,如果成功则不会尝试进行身份验证。 So you want to deny anonymous requests:所以你想拒绝匿名请求:

<authentication mode="Windows" />
<authorization>
  <deny users="?" />
  <allow users="*" />
</authorization>

Note that the order of items in the authorization element is important - they're like any access rules in that they're processed from top to bottom.请注意,授权元素中的项目顺序很重要——它们与任何访问规则一样,都是从上到下处理的。

In this case the?在这种情况下? is anonymous users and * is all users (including anonymous) but as the deny anonymous user statement comes first - they will be denied and never get to see the allow statement which allows everyone else.是匿名用户,* 是所有用户(包括匿名用户),但由于拒绝匿名用户语句首先出现 - 他们将被拒绝并且永远不会看到允许其他人的允许语句。

Also, if this is an ASP.NET MVC 3 web application, there're some quirks to be heedy of - please correct me if I'm wrong as I don't recall all the details right now:此外,如果这是一个 ASP.NET MVC 3 web 应用程序,则需要注意一些怪癖 - 如果我错了,请纠正我,因为我现在不记得所有细节:

  <appSettings>
    <add key="autoFormsAuthentication" value="false" />
  </appSettings>  

The autoFormsAuthentication has to be disabled to enable Windows authentication in an MVC 3 web application - or it was anyway, it might have been fixed by now but as it took quite some time to figure it out, I'm including it here.必须禁用 autoFormsAuthentication 才能在 MVC 3 web 应用程序中启用 Windows 身份验证 - 或者无论如何,它现在可能已经修复,但由于花了很长时间才弄清楚,我将它包括在这里。 The symptom when not disabling it is every authentication request is redirected to the account forms url (which you might not even have).不禁用它时的症状是每个身份验证请求都被重定向到帐户 forms url (您甚至可能没有)。

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

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