Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • JJ 41 posts 83 karma points
    Nov 23, 2017 @ 15:31
    JJ
    0

    Login is redirecting to Login page

    Umbraco 7.7.4

    Upon successful login from a role-based protected node, I'm being redirected to the login screen node again.

    I can successfully load the protected node if I visit it again after logging in.

    Why would it redirect to the login node?

  • Ben Palmer 176 posts 842 karma points c-trib
    Nov 23, 2017 @ 20:47
    Ben Palmer
    1

    Hi JJ,

    This is out-of-the-box behavior actually, but easily fixed. I implemented this on a site I did recently using the following view found at ~/Views/MacroPartials/Login.cshtml:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @using System.Web.Mvc.Html
    @using ClientDependency.Core.Mvc
    @using Umbraco.Web
    @using Umbraco.Web.Models
    @using Umbraco.Web.Controllers
    
    @{
        var loginModel = new LoginModel();
    
        Html.EnableClientValidation();
        Html.EnableUnobtrusiveJavaScript();
        Html.RequiresJs("/umbraco_client/ui/jquery.js");
        Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.min.js");
        Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.unobtrusive.min.js");
    }
    
    @* NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed *@
    @Html.RenderJsHere()
    
    @using (Html.BeginUmbracoForm<UmbLoginController>("HandleLogin"))
    {
        if (!Members.IsLoggedIn())
        {
            @Html.ValidationSummary("loginModel", true)
            @Html.HiddenFor(m => loginModel.RedirectUrl)
    
            <fieldset>
    
                @Html.ValidationSummary("loginModel", true)
    
                @Html.LabelFor(m => loginModel.Username)
                @Html.TextBoxFor(m => loginModel.Username)
                @Html.ValidationMessageFor(m => loginModel.Username)
                <br/>
    
                @Html.LabelFor(m => loginModel.Password)
                @Html.PasswordFor(m => loginModel.Password)
                @Html.ValidationMessageFor(m => loginModel.Password)
                <br/>
                <a href="forgotten-your-password" class="password-reset">Forgotten your password?</a>
                <button>Login</button>
            </fieldset>
        }
        else
        {
            if (Request.UrlReferrer != null && !Request.UrlReferrer.AbsolutePath.IsNullOrWhiteSpace())
            {
                Response.Redirect(Request.UrlReferrer.AbsolutePath);
            }
            else
            {
                Response.Redirect("/");
            }
        }
    }
    

    So I can't remember how close this is to the view that Umbraco generates but the key points are first checking if the member is logged in:

    if (!Members.IsLoggedIn())
        {
    
    }
    

    If they are, show the login form, otherwise, they're logged in and you can perform a redirect:

    if (Request.UrlReferrer != null && !Request.UrlReferrer.AbsolutePath.IsNullOrWhiteSpace())
    {
        Response.Redirect(Request.UrlReferrer.AbsolutePath);
    }
    else
    {
        Response.Redirect("/");
    }
    

    This checks to see if there was a referrer set, if so, redirects to that page, otherwise redirects off to the homepage.

    Hope that helps!

  • JJ 41 posts 83 karma points
    Nov 24, 2017 @ 10:30
    JJ
    0

    Thanks, works perfect.

    Odd "default" behaviour though, but hey ho!

Please Sign in or register to post replies

Write your reply to:

Draft