9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【ASP.NET Identity】メニューに表示される項目を、ユーザーのロールによって変える方法

Posted at

CodeZineさんのASP.NET Identity入門の記事を読みながら、
ASP MVC5でWebアプリケーションを作成した時に迷ったところがあったのでメモ2

ASP.NET Identityでは簡単にロールを作ることができます。
そこでロールでメニューを分けたくなりなりました。

Razor構文で書く

@{
<ul>
  <li>通常メニュー</li>
  if (Request.IsAuthenticated && HttpContext.Current.User.IsInRole("User")) {
    <li>お客様メニュー</li>
  }

  if (Request.IsAuthenticated && HttpContext.Current.User.IsInRole("Admin")) {
    <li>管理者メニュー</li>
  }
</ul>
}

Request.IsAuthenticated 認証されているかどうかを確認しています。
HttpContext.Current.User.IsInRole() ユーザーのロールを確認しています。

これでユーザーアカウントに設定されているロールで、表示される内容が変わります。

この他にも色んなことをやりたくなってきました・・・

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?