asp.net MVC3 razor: display actionlink based on user role

后端 未结 6 1068
臣服心动
臣服心动 2020-11-30 18:57

I\'m new to MVC. I want to be able to hide some actionlinks for some users. Say I have a \"create\" actionlink which I only want administrators to see and click. I want to u

相关标签:
6条回答
  • 2020-11-30 19:10

    I have in the past created a helper function to only return output when a criteria is met like this:

    public static MvcHtmlString If(this MvcHtmlString value, bool evaluation)
    {
         return evaluation ? value : MvcHtmlString.Empty;
    }
    

    so you can use this:

    @Html.ActionLink("Create New", "Create").If(User.IsInRole("Administrators"))
    

    This way it is legible and short

    0 讨论(0)
  • 2020-11-30 19:16

    If you want a code block, that would do in the view :

    @if (Roles.IsUserInRole("Administrators"))
    {
      <li>@Html.ActionLink("Create New", "Create")</li>
    }
    
    0 讨论(0)
  • 2020-11-30 19:20

    I've modified Richard's code to provide an optional MvcHtmlString parameter to return if the evaluation is false.

    public static MvcHtmlString If(this MvcHtmlString value, bool evaluation, MvcHtmlString falseValue = default(MvcHtmlString))
            {
                return evaluation ? value : falseValue;
            }
    
    0 讨论(0)
  • 2020-11-30 19:26

    The solution suggested by Richard is really beautiful, though as Matthieu noted someone might need extra html code to be rendered (or not rendered) as well. Thus Matthieu's solution seems to be more widely applicable, I would just centralize the logic regarding which users are considered admins in extension method.

    Extension method:

    public static bool IsAdmin(this WebViewPage page)
    {
        return page.User.IsInRole(@"Domain\ProjectAdmins");
    }
    

    Usage:

    @if (this.IsAdmin())
    {
        <p>
            @Html.ActionLink("Create New Version", "Create")
        </p>
    }
    
    0 讨论(0)
  • 2020-11-30 19:33

    You can add a function to App_Code/ViewFunctions.cshtml (create if missing)

    @using System.Web.Mvc;
    @functions{
    public static object ConditionalActionLink(object actionLink, ICollection<string> arrAuthUsers)
    {
        bool objIsVisible = arrAuthUsers
            .Select(s => User.IsInRole(s))
            .Where(s => s.Equals(true))
            .Any();
    
        return (objIsVisible)
            ? actionLink
            : MvcHtmlString.Empty;
    }
    

    To use that function just add following code to the view.

    @ViewFunctions.ConditionalActionLink(
    @Html.ActionLink("TextToDisplay", "SomeAction", new { Area = "SomeArea", Controller = "SomeController" }), 
    new string[] { "administrator","jDoe", "someOtherUser" })
    
    0 讨论(0)
  • 2020-11-30 19:34

    If you haven't enabled roleManager and you still want to check User's role, you can do it like this:

     @if (@User.IsInRole("administrator"))
     {
       @Html.ActionLink("Create New Version", "Create")
     }
    

    What we are doing it here is, we are just accessing user's properties. This can useful if you are managing roles yourself.

    0 讨论(0)
提交回复
热议问题