フォーム モデルでは、現在ログインしているユーザーを次の方法で取得していました。
Page.CurrentUser
ASP.NET MVC のコントローラー クラス内で現在のユーザーを取得するにはどうすればよいですか?
フォーム モデルでは、現在ログインしているユーザーを次の方法で取得していました。
Page.CurrentUser
ASP.NET MVC のコントローラー クラス内で現在のユーザーを取得するにはどうすればよいですか?
コントローラ内からユーザーを取得する必要がある場合はUser
、Controllerのプロパティを使用します。ビューから必要な場合は、特に必要なものをに入力しますViewData
。または、のプロパティだと思うので、Userに電話することもできますViewPage
。
試してみてくださいHttpContext.Current.User
。
Public 共有プロパティ Current() System.Web.HttpContext System.Web.HttpContext
のメンバー として概要:
現在の HTTP 要求の System.Web.HttpContext オブジェクトを取得または設定します。戻り値:
現在の HTTP 要求の System.Web.HttpContext
私が使う:
Membership.GetUser().UserName
これが ASP.NET MVC で機能するかどうかはわかりませんが、試してみる価値はあります:)
ログインするユーザー名:System.Web.HttpContext.Current.User.Identity.Name
ユーザー名:
User.Identity.Name
ただし、ID だけを取得する必要がある場合は、次を使用できます。
using Microsoft.AspNet.Identity;
したがって、ユーザー ID を直接取得できます。
User.Identity.GetUserId();
ASP.NET MVC 4 に組み込まれた単純な認証を使用して作成されたユーザー ID をフィルター処理のためにコントローラーで参照するため (これは、データベースを最初に使用し、Entity Framework 5 を使用してコード ファースト バインディングを生成し、テーブルがそのように構造化されている場合に役立ちます)。 userID への外部キーが使用されている場合)、次を使用できます。
WebSecurity.CurrentUserId
usingステートメントを追加したら
using System.Web.Security;
このページはあなたが探しているものかもしれません:
Using Page.User.Identity.Name in MVC3
あなただけが必要User.Identity.Name
です。
を使用しSystem.Security.Principal.WindowsIdentity.GetCurrent().Name
ます。
これにより、現在ログインしている Windows ユーザーが取得されます。
ログインページ内にいる場合、たとえばLoginUser_LoggedInイベントでは、Current.User.Identity.Nameは空の値を返すため、yourLoginControlName.UserNameプロパティを使用する必要があります。
MembershipUser u = Membership.GetUser(LoginUser.UserName);
価値があるのは、ASP.NET MVC 3 では、現在の要求に対してユーザーを返す User を使用することだけです。
IPrincipal currentUser = HttpContext.Current.User;
bool writeEnable = currentUser.IsInRole("Administrator") ||
...
currentUser.IsInRole("Operator");
var ticket = FormsAuthentication.Decrypt(
HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName].Value);
if (ticket.Expired)
{
throw new InvalidOperationException("Ticket expired.");
}
IPrincipal user = (System.Security.Principal.IPrincipal) new RolePrincipal(new FormsIdentity(ticket));
イントラネット上の Active Directory で作業している場合は、次のヒントを参考にしてください。
(Windows サーバー 2012)
Web サーバーで AD と通信するものを実行するには、多くの変更と忍耐が必要です。Web サーバーとローカル IIS/IIS Express で実行する場合、AppPool の ID で実行されるため、サイトにアクセスした人を偽装するように設定する必要があります。
ASP.NET MVC アプリケーションがネットワーク内の Web サーバーで実行されている場合に、Active Directory で現在ログインしているユーザーを取得する方法:
// Find currently logged in user
UserPrincipal adUser = null;
using (HostingEnvironment.Impersonate())
{
var userContext = System.Web.HttpContext.Current.User.Identity;
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["AllowedDomain"], null,
ContextOptions.Negotiate | ContextOptions.SecureSocketLayer);
adUser = UserPrincipal.FindByIdentity(ctx, userContext.Name);
}
//Then work with 'adUser' from here...
AD 情報を取得するためのホスティング環境として機能するように、「アクティブ ディレクトリ コンテキスト」に関連するすべての呼び出しを次のようにラップする必要があります。
using (HostingEnvironment.Impersonate()){ ... }
impersonate
また、web.config で true に設定する必要があります。
<system.web>
<identity impersonate="true" />
web.config で Windows 認証を有効にする必要があります。
<authentication mode="Windows" />
まだこれを読んでいる人がいる場合は、cshtml ファイルにアクセスするために、次の方法で使用しました。
<li>Hello @User.Identity.Name</li>