如何在IIS7 +上的asp.net webApp中获得Windows用户 [英] How to get Windows user in asp.net webApp on IIS7+

查看:57
本文介绍了如何在IIS7 +上的asp.net webApp中获得Windows用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个部署在远程IIS7 +服务器上的asp.net 4.5 Web窗体Web应用程序,我想获取用户的域\用户名以填充数据库列.

I have an asp.net 4.5 web forms web app deployed on a remote IIS7+ server and I want to get the domain\username of the user to populate a database column.

我的web.config具有以下内容:

My web.config has the following:

<system.web>
   <authentication mode="Windows" />
   <identity impersonate="true" /> <!-- I have tried with and without impersonate-->
   <authorization>
         <deny users="?"/>
   </authorization>
   ...other stuff
</system.web>
<system.webServer>
   <security>
     <authentication>
       <anonymousAuthentication enabled="false" />
       <windowsAuthentication enabled="true" />
     </authentication>
   </security>

   <modules>
      <remove name="FormsAuthentication" />
   </modules>

以下是我尝试过的一些事情:

Here are some of the things I have tried:

String username = Membership.GetUser().UserName;
                = HttpContext.Current.User.Identity.Name;
                = Page.User.Identity;
                = User.Identity.Name;
                = Environment.Name; // Or something similar to this I don't remember exactly.

其他注意事项:

我的Web应用程序在服务器上启用了Windows身份验证.

Windows authentication is enabled on the server for my web app.

我一直在获取服务器名称而不是用户名.

I keep getting the server name instead of the username.

此外,我的Web应用程序没有登录.用户被限制访问服务器,但是如果他们可以访问服务器,那么他们将自动有权访问我的Web应用.

Also, there is no logon for my web app. Users are restricted access to the server but if they can access the server then they automatically have access to my web app.

[UPDATE]

有趣的突破,如果我进行内联

Interesting breakthrough, if I do an inline

 <% Response.Write(HttpContext.Current.User.Identity.Name.ToString()); %>

然后将myDomain \ username写入我的页面.但是,如果我在同一代码服务器端执行操作,它将返回服务器名称.为什么会返回不同的东西?

then myDomain\username is written to my page. However, if I do the same code server side it returns the Server Name. Why would it return something different?

我尝试了下面的代码,但是它仍然返回服务器名称,我猜是因为内联运行在客户端,而控件运行在服务器端.

I have tried the below code but it still returns the Server name, I'm guessing its because the inline runs client-side and the controls run server side.

<asp:Label ID="LabelID" Text=" <% HttpContext.Current.User.Identity.Name.ToString(); %>" />

然后在代码背后

String curUser = LabelID.Text;

推荐答案

请参阅: http://forums.asp.net/t/1121780.aspx?从+ a + web +应用程序获取+ a + users + DOMAIN + username +

要获取asp.net aspx页面的当前用户名,请使用

To get the current userid name of an asp.net aspx page use

System.Security.Principal.WindowsIdentity.GetCurrent().Name 

要使用以下任何一种方法来获取实际登录的人:

To get the actual person who's logged in use any of these:

HttpContext.Current.User.Identity.Name
Page.User (wraps HttpContext.Current.User.Identity.Name)
Request.ServerVariables("logon_user")

您可能还需要:

impersonate=true

在您的web.config文件中

in your web.config file

这篇文章还可以为您提供帮助:内置的助手,用于解析User.Identity.命名为Domain \ Username

This post may also help you: Built-in helper to parse User.Identity.Name into Domain\Username

这篇文章显示了IIS 7的幕后情况:如何获取当前用户的Windows身份

This post shows what's happening under the covers in IIS 7: How to get at the current users windows identity

希望这篇文章也会使您对试错结果的含义有所了解:如何获取Windows用户名当身份冒充为真"时,在asp.net中?

This post will hopefully also give you an idea of what the results of your trial and error mean: How to get Windows user name when identity impersonate="true" in asp.net?

您可能还需要在web.config中添加以下内容

You may also need the following in your web.config

<authorization>
    <deny users = "?" /><!-- This denies access to the Anonymous user -->
    <allow users ="*" /><!-- This allows access to all users -->
</authorization>

您观察到的行为暗示您正在执行

The behavior you are observing implies you are executing

HttpContext.Current.User.Identity.Name

在IIS的处理管道应用程序生命周期中,即在进行身份验证之前,您称其为服务器端"还为时过早.

On what you are calling "the server side" too early in IIS's processing pipeline application lifecycle i.e. before authentication has taken place. The

 Response.Write( 

是应用程序生命周期的最后一个方面.看一下Microsoft的IIS7生命周期文档: http://msdn.microsoft.com/en-us/library/bb470252.aspx

is one of the last aspects of application life cycle. Take a look at Microsoft's IIS7 life cycle documentation: http://msdn.microsoft.com/en-us/library/bb470252.aspx

  1. 验证请求.
  2. 执行URL映射.
  3. 引发BeginRequest事件.
  4. 引发AuthenticateRequest事件.
  5. 引发PostAuthenticateRequest事件.
  6. 引发AuthorizeRequest事件.
  7. 引发PostAuthorizeRequest事件....

只有在此之后,才能真正安全地进行所需的处理

Its only after this that it will really be safe to do the processing you need to

这篇关于如何在IIS7 +上的asp.net webApp中获得Windows用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆