asp.netでWindows認証を使用してユーザー名を取得する方法とは?

Windows認証でユーザー名を取得したい

実は、quot;Sign in as different user"を実装しており、このボタンをクリックすると、Windowsセキュリティが表示され、認証情報を与えることができます。

その際、他のクレデンシャルを与えると、現在のユーザー名だけが取得されます。 Windowsセキュリティから与えられた認証情報のユーザー名を取得するにはどうすればいいでしょうか?

IISでアプリケーションをホストしている場合、匿名認証は無効で、Windows認証は有効になっています。

web.config:

<system.web>
    <compilation debug="true" targetFramework="4.0" />
  <identity impersonate="true"/>
  <authorization>
      <allow users="*"/>
      <deny users="*"/>
  </authorization>
</system.web>
<system.webServer>
    <directoryBrowse enabled="true" />
    <security>
        <authentication>
            <anonymousAuthentication enabled="false" />
            <windowsAuthentication enabled="true" />
        </authentication>
    </security>

</system.webserver&gt;

.cs

ここでは、常にデフォルトのユーザー名を取得しています。

string fullName = Request.ServerVariables["LOGON_USER"];

何かアイデアはありますか?ありがとうございました。

Windows認証でユーザー'の WindowsIdentity オブジェクトを取得するのは以下の方法です:

WindowsIdentity identity = HttpContext.Current.Request.LogonUserIdentity;

で、identity.Nameのようなユーザーに関する情報を取得することができます。

なお、これらのコードを実行するには、HttpContextが必要です。

解説 (2)

これらは、IIS構成に応じて、アクセスできるさまざまな変数とその値です。

シナリオ1:なりすましをオフにしたIISの匿名認証。

HttpContext.Current.Request.LogonUserIdentity.Name    SERVER1\IUSR_SERVER1 
HttpContext.Current.Request.IsAuthenticated           False                    
HttpContext.Current.User.Identity.Name                –                        
System.Environment.UserName                           ASPNET                   
Security.Principal.WindowsIdentity.GetCurrent().Name  SERVER1\ASPNET

シナリオ2: IISでのWindows認証、なりすまし。

HttpContext.Current.Request.LogonUserIdentity.Name    MYDOMAIN\USER1   
HttpContext.Current.Request.IsAuthenticated           True             
HttpContext.Current.User.Identity.Name                MYDOMAIN\USER1   
System.Environment.UserName                           ASPNET           
Security.Principal.WindowsIdentity.GetCurrent().Name  SERVER1\ASPNET

シナリオ3: IISでの匿名の認証、なりすまし。

HttpContext.Current.Request.LogonUserIdentity.Name    SERVER1\IUSR_SERVER1 
HttpContext.Current.Request.IsAuthenticated           False                    
HttpContext.Current.User.Identity.Name                –                        
System.Environment.UserName                           IUSR_SERVER1           
Security.Principal.WindowsIdentity.GetCurrent().Name  SERVER1\IUSR_SERVER1 

シナリオ4: IISでのWindows認証、なりすまし。

HttpContext.Current.Request.LogonUserIdentity.Name    MYDOMAIN\USER1
HttpContext.Current.Request.IsAuthenticated           True          
HttpContext.Current.User.Identity.Name                MYDOMAIN\USER1
System.Environment.UserName                           USER1         
Security.Principal.WindowsIdentity.GetCurrent().Name  MYDOMAIN\USER1

Legend
SERVER1 \ ASPNET:サーバーで実行中のプロセスのID。
SERVER1 \ IUSR_SERVER1:IIS.
で定義された匿名のゲストユーザー。 MYDOMAIN \ USER1:リモートクライアントのユーザー。

ソース

解説 (3)

これで大丈夫なはずです:

User.Identity.Name

IDENTITYIPrincipal` を返します。

以下は、Microsoftのドキュメントへのリンクです。

解説 (0)

以下のリンクのこの紳士が正しく説明したように、それはアプリケーションの構成とIISに依存します。 以下の彼の記事を参照してください。

http://richhewlett.com/2011/02/15/getting-a-users-username-in-asp-net/

解説 (1)

以下のコードが原因で、新しいクレデンシャルが取得できないのだと思います。

string fullName = Request.ServerVariables["LOGON_USER"];

カスタムログインページ**を試してみてください。

解説 (2)

ユーザー名は次のようになります。

var userName = HttpContext.Current.Request.LogonUserIdentity?.Name;
解説 (0)