I have code in one of my views that looks like this below where if you are logged in, it shows you the welcome notice and if you are not logged in, it shows you a link to the logon page.
<%
if (!Request.IsAuthenticated)
{
%>
<%= Html.ActionLink("Log On", "LogOn", "Account")%>
<%
}
else
{
%>
<img src="../images/newspic.gif" width="423" height="194"><br /><br />
<%
}
%>
what i want is that if you are not logged in, i would like it to redirect to the login page automatically (as why have that extra step)
any suggestions on how i would go about automatically redirecting to another page from within this view above.
Your View should only be responsible for displaying your data. Your controller should handle redirection logic.
The Authorize Attribute has already been created for this purpose. Add it to your controller action method like:
Or alternatively it can be added to the Controller to apply to all Actions:
Then in your web.config set something like
If the user is authorized they will be shown your ‘Home’ view. If they are not authorized they will be redirected to ‘/Login’