質問

フォームモデルでは、次の方法で現在のログインユーザーを取得していました。

Page.CurrentUser

ASP.NET MVCのコントローラークラス内で現在のユーザーを取得するにはどうすればよいですか

役に立ちましたか?

解決

コントローラー内からユーザーを取得する必要がある場合は、コントローラーの User プロパティを使用します。ビューから必要な場合は、特に必要なものを ViewData に入力するか、 ViewPage のプロパティだと思うのでUserを呼び出すことができます。

他のヒント

User が機能すること、つまり User.Identity.Name または User.IsInRole(" Administrator")

HttpContext.Current.User を試してください。

  

パブリック共有プロパティCurrent()As   System.Web.HttpContext
      System.Web.HttpContextのメンバー

     

概要:
  現在のHTTP要求のSystem.Web.HttpContextオブジェクトを取得または設定します。

     

戻り値:
  現在のSystem.Web.HttpContext   HTTPリクエスト

次のようにASP.NET MVC4でユーザーの名前を取得できます。

System.Web.HttpContext.Current.User.Identity.Name

これは本当に古いことはわかっていますが、ASP.NET MVCを使い始めたばかりなので、2セントを費やすと思いました:

  • Request.IsAuthenticated は、ユーザーが認証されているかどうかを示します。
  • Page.User.Identity は、ログインしているユーザーのIDを提供します。

使用:

Membership.GetUser().UserName

これがASP.NET MVCで機能するかどうかはわかりませんが、試してみる価値はあります:)

ログインユーザー名の取得: System.Web.HttpContext.Current.User.Identity.Name

フィルター処理のためにコントローラーのASP.NET MVC 4に組み込まれた単純な認証を使用して作成されたユーザーIDを参照するにはuserIDへの外部キーが使用されるように構造化されています)、使用できます

WebSecurity.CurrentUserId

usingステートメントを追加したら

using System.Web.Security;

UserName with:

User.Identity.Name

ただし、IDだけを取得する必要がある場合は、次を使用できます。

using Microsoft.AspNet.Identity;

したがって、ユーザーIDを直接取得できます:

User.Identity.GetUserId();

このページはあなたが探しているものです:
MVC3でPage.User.Identity.Nameを使用

必要なのは、 User.Identity.Name です。

System.Security.Principal.WindowsIdentity.GetCurrent()。Name を使用します。

これにより、現在ログインしているWindowsユーザーが取得されます。

次のコードを使用して、ASP.Net MVCで現在ログインしているユーザーを取得できます。

var user= System.Web.HttpContext.Current.User.Identity.GetUserName();

また

var userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; //will give 'Domain//UserName'

Environment.UserName - Will Display format : 'Username'

価値のあるものとして、ASP.NET MVC 3では、現在のリクエストに対してユーザーを返すUserを使用できます。

ログインページ内にいる場合、たとえばLoginUser_LoggedInイベントでは、Current.User.Identity.Nameは空の値を返すため、yourLoginControlName.UserNameプロパティを使用する必要があります。

MembershipUser u = Membership.GetUser(LoginUser.UserName);
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 Server 2012)

WebサーバーでADと通信するものを実行するには、多くの変更と忍耐が必要です。 WebサーバーとローカルIIS / IIS Expressで実行する場合、AppPoolのIDで実行するため、サイトをヒットした人になりすますように設定する必要があります。

ASP.NET MVCアプリケーションがネットワーク内のWebサーバーで実行されているときに、アクティブなディレクトリに現在ログインしているユーザーを取得する方法:

// 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()){ ... }

web.configで impersonate をtrueに設定する必要もあります:

<system.web>
    <identity impersonate="true" />

web.configでWindows認証がオンになっている必要があります:

<authentication mode="Windows" />

次のコードを使用できます:

Request.LogonUserIdentity.Name;

Asp.net Mvc Identity 2では、次の方法で現在のユーザー名を取得できます。

var username = System.Web.HttpContext.Current.User.Identity.Name;

IISマネージャーの[認証]で、次を無効にします。 1)匿名認証 2)フォーム認証

次に、テストとサーバーの展開を処理するために、コントローラーに次を追加します。

string sUserName = null;
string url = Request.Url.ToString();

if (url.Contains("localhost"))
  sUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
else
  sUserName = User.Identity.Name;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top