ASP.Net MVC 5 Check User Role for _Layout

Posted by: tranhau102495 on 1 April 2019, 8:11 pm EST

  • Posted 1 April 2019, 8:11 pm EST

    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.

    01

    @if (Request.IsAuthenticated)

    02

    {

    03



  • 04

    Assign Roles

    05



  • 06



  • 07

    View Roles

    08



  • 09

    } else if (Request.IsAuthenticated && User.IsInRole(“User”))

    10

    {

    11



  • 12

    user Else clause

    13



  • 14

    } else if (Request.IsAuthenticated && User.IsInRole(“Guest”))

    15

    {

    16



  • 17

    Guest Else clause

    18



  • 19

    } else

    20

    {

    21



  • 22

    else else!! Else clause

    23



  • 24

    }

  • Posted 3 April 2019, 2:24 am EST

    Hi,

    Please refer to the following article, this might be helpful in your requirement fulfillment.

    https://www.c-sharpcorner.com/article/dynamic-menu-bind-depends-on-user-role-in-asp-mvc/

    Regards,

    Manish Gupta

  • Need extra support?

    Upgrade your support plan and get personal unlimited phone support with our customer engagement team

    Learn More

    Forum Channels