Logon Strategy Redirection

I have a link on a page that allows the user to perform a specific action if they are logged in. If they are not logged in, I want the link to them to go to the login page first. This is pretty common. What is the best way to do this? I am currently doing this, but I do not like this:

<% 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 that view. Is it better to simply redirect the "StartPuzzle" action if you are not logged in?

+3
source share
4 answers

create an htmlhelpers extension method that will authenticate the user and return a single link or something like this:

In your opinion:

<%=Html.RenderLoginLink()%>

htmlhelper:

if(autorized)
{ 
   return Html.ActionLink("Start Puzzle", "StartPuzzle", "Puzzles");
}
else
{
   return Html.ActionLink("Start Puzzle", "Login", "Account");
}
+2

MVC :

+1

, . , , , . URL .

+1

Place the Authorize attribute on top of the StartPuzzle action in the controller. Thus, a user who does not authenticate will be automatically redirected to the login page, where returnUrl indicates what he accessed when he was not registered. Thus, after a successful login, he will be redirected back to the launch of the puzzle.

+1
source

Source: https://habr.com/ru/post/1708833/


All Articles