0

I've created movies (using the movies tutorial) and I want to hide the "create new" link so only a logged in user can see it.

I've tried searching for answers but can't find anything that helps me.

Any help would be appreciated.

  • 1
    Welcome to Stack Overflow! Your question could be good and answered, but you need to provide some more details about what you've tried already, i.e. put in the code you've written that isn't working (but don't put in a lot of extra detail!). Review [how to write a minimal viable complete example](http://stackoverflow.com/help/mcve) for more information. – daphtdazz Dec 06 '16 at 14:49

2 Answers2

0

You could use something like the following. Use Request.IsAuthenticated to determine if the user is logged in.

@if (Request.IsAuthenticated)
{
   <a href="@Url.Action("Index", "Manage")">
   <i class="fa fa-user"></i>
   <span class="text">Profile</span>
   </a>
}
JasonlPrice
  • 187
  • 2
  • 13
0

The easiest way to do it would be to wrap the ActionLink in an if clause like this

    @if (User.Identity.IsAuthenticated)
    {
        @Html.ActionLink("link text", "action name");
    }

but that's not really reusable and it mixes business concerns with your UI code.

Take a look at the this answer

How to override the ActionLink behavior

It's a real nice way to encapsulate the permission checking. Then just use @Html.AuthorizeActionLink in place of ActionLink.

Fran
  • 6,440
  • 1
  • 23
  • 35