1 Replies - 152 Views - Last Post: 31 March 2019 - 06:07 PM

#1 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 216
  • Joined: 01-July 12

ASP.Net MVC 5 Check User Role for _Layout

Posted 31 March 2019 - 04:29 PM

Hello everyone I am trying to check what Role my user is in with the default setup.
I thought I would only need 'User.IsInRole("User")' for in my View but its not so easy.
I want to display certain links depending on the role for the user.
Here is what I have and I tried a few different options.
I have the default setup database and some added tables that dont matter for this part.
Request.IsAuthenticated works for login.

 @if (Request.IsAuthenticated)
                    {
                        <li class="nav-item">
                            <a class="nav-link" href="#">Assign Roles</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">View Roles</a>
                        </li>
} else if (Request.IsAuthenticated && User.IsInRole("User"))
                    {
                        <li class="nav-item">
                            <a class="nav-link" href="#">user Else clause</a>
                        </li>
                    } else if (Request.IsAuthenticated && User.IsInRole("Guest"))
                    {
                        <li class="nav-item">
                            <a class="nav-link" href="#">Guest Else clause</a>
                        </li>
                    } else
                    {
                        <li class="nav-item">
                            <a class="nav-link" href="#">else else!! Else clause</a>
                        </li>
                    }



The 'else' works when no user is logged in.

Is This A Good Question/Topic? 0
  • +

Replies To: ASP.Net MVC 5 Check User Role for _Layout

#2 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 216
  • Joined: 01-July 12

Re: ASP.Net MVC 5 Check User Role for _Layout

Posted 31 March 2019 - 06:07 PM

This is what i ended up doing to for my solution

@if (User.IsInRole("User"))
                    {
                        <li class="nav-item">
                            <a class="nav-link" href="#">user Else clause</a>
                        </li>
                    } 
                    @if (User.IsInRole("Guest"))
                    {
                        <li class="nav-item">
                            <a class="nav-link" href="#">guest Else clause</a>
                        </li>
                    }

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1