質問

現在ログインしているユーザーのユーザー名を取得したい。以下のコードを使用していますが、IISアプリケーションプールidで資格情報が使用されているユーザーを返します。Mvc webサイトはサーバーにデプロイされます。

私は使用しました

string userid = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

string userid = Environment.UserName;

string userid = System.DirectoryServices.AccountManagement.UserPrincipal.Current.GivenName.ToString();

string userid = System.Web.HttpContext.Current.User.Identity.Name.ToString()

しかし、何も私に正しい答えを与えていないようです。

私はのコンストラクタでユーザー名を取得する必要があります HomeController.

以下は私のウェブです。コンフィグ

<authentication mode="Windows" />
<authorization>
    <allow users="*" />
</authorization>
役に立ちましたか?

解決

ただの代わりに

string userid = System.Web.HttpContext.Current.User.Identity.Name.ToString()

これを使用すると、これは正常に動作します

string userid = HttpContext.Current.User.Identity.Name.ToString() 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top