简体   繁体   中英

ASP.Net MVC 5 Check User Role For _Layout

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.

I tried user.isinrole and request.isauthenticated but nether worked for this instance with my view

@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>                        
                        <li class="nav-item">
                            <a class="nav-link" href="#">Reset Password</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.

You can add extension method for Principal like this

public static class PrincipalExtensions
{
    public static bool IsInAllRoles(this IPrincipal principal, params string[] roles)
    {
        return roles.All(r => principal.IsInRole(r));
    }

    public static bool IsInAnyRoles(this IPrincipal principal, params string[] roles)
    {
        return roles.Any(r => principal.IsInRole(r));
    }
}

And use

// user must be assign to all of the roles  
if(User.IsInAllRoles("Admin","Manager","YetOtherRole"))
{
    // do something
} 

Read more at Usage of User.IsInRole() in a View

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>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM