Asp – Redirecting to login page strategy

I have a link on the page that allows the user to perform a certain action if they are logged in. If they are not logged in I want the link to direct them to the login page first. This pretty common. What's the best way to do this? Currently I'm doing this but I don't like it:

<% if(Model.IsUserAuthenticated){ %>
<%= Html.ActionLink("Start Puzzle", "StartPuzzle", "Puzzles")%>
<%} else { %>
<%= Html.ActionLink("Start Puzzle", "Login", "Account")%>
<%} %>

You get the idea. I don't really like having logic in the view like this. Is it better to just have the "StartPuzzle" action redirect if not logged in?

Best Solution

create htmlhelpers extension method which will check if user is authenticated and return one link or another...something like this:

in your view:


and in you htmlhelper:

   return Html.ActionLink("Start Puzzle", "StartPuzzle", "Puzzles");
   return Html.ActionLink("Start Puzzle", "Login", "Account");