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>;
.cs
ここでは、常にデフォルトのユーザー名を取得しています。
string fullName = Request.ServerVariables["LOGON_USER"];
何かアイデアはありますか?ありがとうございました。
これらは、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
:リモートクライアントのユーザー。
。
ソース。
以下のリンクのこの紳士が正しく説明したように、それはアプリケーションの構成とIISに依存します。 以下の彼の記事を参照してください。
http://richhewlett.com/2011/02/15/getting-a-users-username-in-asp-net/。