Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 844 posts 2220 karma points
    Jun 20, 2014 @ 09:30
    Claushingebjerg
    2

    Member login doesn't redirect to protected node after login

    Im using members on a v7 for the first time.

    In previous versions you would protect a page, and then make login page.
    On the protected page you would choose the "login" page as login page.

    When hitting the protected page you would be redirected to the login page.
    When succesfully logged in, you would be redirected back to the now unprotected page.

    In 7 (7.1.4), when doing the above, you are correctly redirected to the login page when you hit the protected page, but you just stay on the login page after logging in.
    When navigating to the protected page you now have access, so you are logged in.
    But why doesnt it redirect the user here?

    I'm using the standard membership and razor login macro supplied with umbraco.

    Is there a new way of achieving this "correct" behaviour?

     

  • Dave Woestenborghs 3217 posts 10834 karma points MVP 4x admin c-trib
    Jun 20, 2014 @ 13:46
    Dave Woestenborghs
    1

    I have used this gist and it works like you want :

    https://gist.github.com/anonymous/4336141

  • Steve Morgan 1030 posts 3195 karma points c-trib
    Jun 20, 2014 @ 15:01
    Steve Morgan
    107

    Hi,

     

    I've blogged about this - I've an updated login Partial View Macro on the site which will avoid having to use a Surface Controller (though Dave's suggestion is a perfect solution, especially if you plan to extend members). 

    Using only Razor - see section 4.3 - remember to change the node ID:

    http://siempresolutions.co.uk/blog/Umbraco_Members_Protected_Area_of_Website

    Build your own Surface Controller:

    http://siempresolutions.co.uk/blog/Umbraco_Members_Protected_Area_of_Website_Part_2_Using_MVCMembers_Controller

    HTH

    Steve

  • Claushingebjerg 844 posts 2220 karma points
    Jun 20, 2014 @ 15:19
    Claushingebjerg
    0

    You made my day, thanks!

  • umbraco idiot 8 posts 52 karma points
    Sep 07, 2014 @ 18:05
    umbraco idiot
    0

    Steve, your post is awesome. But I can't get it to work....

    It ALWAYS redirects to the home page (root page).

    Any ideas? Thanks.

     

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @using System.Web.Mvc.Html
    @using ClientDependency.Core.Mvc
    @using Umbraco.Web
    @using Umbraco.Web.Models
    @using Umbraco.Web.Controllers


    @{
        var loginNodeID = 1228;
        var loginModel = new LoginModel();
        var loginStatusModel = new LoginStatusModel();
       
        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");
    }

    @Html.RenderJsHere()

    @{
        var checkUrl = HttpContext.Current.Request.Url.AbsolutePath.ToString();
       
        @*// add a trailing / if there isn't one (you can access http://mydomain.com/login or http://mydomain.com/login/*@
        if (@checkUrl[checkUrl.Length -1] != '/') {
            checkUrl = checkUrl + "/";
        }
     
        @* if we don't have a session variable and have a request URL then store it *@
        @* we have to store it because if user tries an incorrect login then Current.Request.Url will show /umbraco/RenderMvc *@
        if (HttpContext.Current.Request.Url != null && HttpContext.Current.Request.Url.AbsolutePath.ToString() != "/umbraco/RenderMvc" && HttpContext.Current.Session["redirectURL"] == null) {
            if (checkUrl !=  @Umbraco.Content(loginNodeID).Url) {
                HttpContext.Current.Session["redirectURL"] = HttpContext.Current.Request.Url.ToString();
            }
        }

        if (loginStatusModel.IsLoggedIn)
        {
            var redirectUrl = (string)HttpContext.Current.Session["redirectURL"];
           
            var currentUrl = HttpContext.Current.Request.Url.ToString();
            if (HttpContext.Current.Session["redirectURL"] != null) {
                // clear the session variable for future logins
                HttpContext.Current.Session["redirectURL"] = null;
                HttpContext.Current.Response.Redirect(redirectUrl);
            }
            else {
                // Nothing in the session so we will go home
                HttpContext.Current.Response.Redirect("/");
            }
        }
        else
        {
            Response.Write(loginStatusModel.IsLoggedIn);
        }
       
        using (Html.BeginUmbracoForm<UmbLoginController>("HandleLogin"))
        {
            <fieldset>
                <legend>Login</legend>
               
                @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 />
       
                <button>Login</button>
            </fieldset> 
        }
    }

  • Steve Morgan 1030 posts 3195 karma points c-trib
    Sep 09, 2014 @ 08:12
    Steve Morgan
    0

    Hi,

     

    What version of Umbraco are you using?

    Sorry, I haven't had any time to look at this. I suspect it's throwing an error and using your home page as the error page. 

     

    Have a look in the error log (found under /app_data/logs) and post the last error out here. It might also be worth writing a quick macro on the homepage which outputs the current user (if any) and the redirect URL (uncomment out the line that clears this out of the session variable in the script above). 

     

  • umbraco idiot 8 posts 52 karma points
    Sep 09, 2014 @ 09:19
    umbraco idiot
    1

    I'm using version 7.0.4.
    I just found out that the UmbLoginController is hardcoded to redirect to the root page after login. That's why it's not working.

    Updated it to version 7.1.6 and it's OK. :)

  • Steve Morgan 1030 posts 3195 karma points c-trib
    Sep 09, 2014 @ 09:56
    Steve Morgan
    0

    That's an interesting find - I'll put a note of warning on my blog post. Thanks!

  • keilo 559 posts 1014 karma points
    Oct 25, 2014 @ 18:17
    keilo
    0

    I just came across the same. Upgrading to 7.1.8 (from 7.1.4) solved the homepage redirect issue.

     

     

  • Matt 91 posts 236 karma points
    Apr 13, 2015 @ 10:58
    Matt
    1

    Still having this same issue on Umbraco 7.2.4.

    Protected a page using public access - created an umbraco node with the login snippet on it and set this as the login page. Created a seperate error page and set this as the error page.

    As mentioned above the member is logged in successfully but it does not redirect anywhere, it stays on the login page.

    Am I using the snippet incorrectly or is it a bug?

  • Biagio Paruolo 1474 posts 1613 karma points c-trib
    Jul 27, 2015 @ 16:20
    Biagio Paruolo
    0

    I've the same problem. This is a big issue.

  • Matthew Kirschner 323 posts 605 karma points
    Apr 13, 2015 @ 15:34
    Matthew Kirschner
    1

    Hi, Matt.

    I've just run into the same issue and managed to get it to work by creating adding a redirectUrl parameter (Content Picker) to the Macro and adding the following code to the Login snippet.

    Put this under the loginModel declaration:

    var loginUrlPart = Umbraco.Content(Model.Content.Id).Url;
    var currentUrl = Request.RawUrl;
    
    /**
     * This conditional redirect is necessary due to Umbraco's use of Public Access.
     * Rather than setting a query string redirect url, Umbraco alters the raw URL to be the referrer.
     * So, if the raw url is anything but the sign in page's url, then we redirect to the raw url.
     * Otherwise, use the macro parameter, redirectUrl.
     */
    // 
    // Rather than
    loginModel.RedirectUrl = currentUrl.Contains(loginUrlPart) ? 
        Umbraco.Content(Model.MacroParameters["redirectUrl"]).Url : 
        currentUrl;
    

    Add this in the @using BeginUmbracoForm block:

    @Html.HiddenFor(m => loginModel.RedirectUrl)
    

    If I remember correctly, the UmbLoginController will listen to a RedirectUrl variable on the login model. The parameter on the Macro we made allows you to set a default redirect in the macro, while the code will redirect to the Public Access page if the url doesn't match up with the login page.

  • John Churchley 272 posts 1256 karma points c-trib
    Apr 13, 2015 @ 18:06
    John Churchley
    2

    I got around it by checking if the user is logged in on the login page then redirect to the previous referreral page if they are. Think this is how it should work hope this helps someone else.

    @if(Members.GetCurrentLoginStatus().IsLoggedIn){
        Response.Redirect(Request.UrlReferrer.AbsolutePath);
    }
  • Russell Cavell 3 posts 74 karma points
    Jul 17, 2018 @ 08:33
    Russell Cavell
    0

    Love it - so easy. Thanks John.

  • Michael 22 posts 61 karma points
    May 26, 2015 @ 11:04
    Michael
    0

    I also encoutered this problem, I decide tol implement the mebership function via MVC, 

    I have some protected nodes. if the customer doesn't login, they will redirect to login page. after login, then redirect the url the user requested before.

     

    @all, have you gotten the solution? Could you give me an idea? Thanks in advanced.

  • aleksandar 14 posts 74 karma points
    Jul 28, 2015 @ 08:45
    aleksandar
    2

    this solve my problem :

    Response.Redirect(Request.RawUrl);

  • Biagio Paruolo 1474 posts 1613 karma points c-trib
    Jul 28, 2015 @ 08:58
    Biagio Paruolo
    0

    How?

    I tried. Could you post your login code? I tried a lot of time and don't login... And i cannot show the private zone to the customer 😡

  • aleksandar 14 posts 74 karma points
    Jul 30, 2015 @ 10:16
    aleksandar
    0
     public class LoginController : SurfaceController
        {
    
            // GET: /Login/
            [HttpGet]
            public ActionResult RenderLogin()
            {
                return View("UserLogin");
            }
    
            // POST: /Login/Create
            [HttpPost]
            public ActionResult Login(LoginModel model)
            {
                if (ModelState.IsValid)
                {
    
                    var isMember = Membership.ValidateUser(model.UserName, model.Password);
                    if (isMember)
                    {
                        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                         Response.Redirect(Request.RawUrl);                    
                    }          
                }
                TempData.Add("LoginErrMsg", "The username or password you entered is incorrect !");
                return RedirectToCurrentUmbracoPage();
    
            }
    
        }
    
  • Biagio Paruolo 1474 posts 1613 karma points c-trib
    Jul 30, 2015 @ 11:09
    Biagio Paruolo
    0

    So, I've to create a controller in app_code. Why Do not work the default?

  • aleksandar 14 posts 74 karma points
    Jul 30, 2015 @ 11:17
    aleksandar
    0

    it works in my case, im not sure why it doesnt work at yours!

  • Biagio Paruolo 1474 posts 1613 karma points c-trib
    Jul 30, 2015 @ 11:21
    Biagio Paruolo
    0

    Do work the default 4 you?

  • aleksandar 14 posts 74 karma points
    Jul 30, 2015 @ 11:23
    aleksandar
    0

    No

  • Biagio Paruolo 1474 posts 1613 karma points c-trib
    Jul 30, 2015 @ 11:28
    Biagio Paruolo
    0

    I think that we need an official doc and report for this. Is there s bug open?

  • Biagio Paruolo 1474 posts 1613 karma points c-trib
    Aug 10, 2015 @ 07:09
    Biagio Paruolo
    0

    I solved.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    
    @using System.Web.Mvc.Html
    @using ClientDependency.Core.Mvc
    @using Umbraco.Web
    @using Umbraco.Web.Models
    @using Umbraco.Web.Controllers
    
    
    
    @{
        var loginNodeID = 1207;
        var loginModel = new LoginModel();
        var loginStatusModel = Members.GetCurrentLoginStatus();
    
        loginModel.RedirectUrl = "/area-privata-b2b";
    
    
    
    }
    
    
    
    @using (Html.BeginUmbracoForm<UmbLoginController>("HandleLogin"))
    {
    
    
                <h3 class="page-header">Login B2B</h3>  
                 @Html.ValidationSummary("loginModel", true)    
        <div>
    
                  <div class="form-group">
                    @Html.LabelFor(m => loginModel.Username)
            @Html.TextBoxFor(m => loginModel.Username, new { @placeholder = "Username", @class="form-control" })
            @Html.ValidationMessageFor(m => loginModel.Username)  
    
                  </div>
                  <div class="form-group">
                        @Html.LabelFor(m => loginModel.Password)
            @Html.PasswordFor(m => loginModel.Password, new { @placeholder = "Password", @class="form-control" })
            @Html.ValidationMessageFor(m => loginModel.Password)
                    </div>
                      @Html.HiddenFor(m => loginModel.RedirectUrl)
                   <button  class="btn btn-primary">Login</button>
                </div>  
    
    
    }
    
  • Craig100 981 posts 2114 karma points c-trib
    Aug 13, 2015 @ 16:06
    Craig100
    0

    @Steve Morgan, Excellent tutorial.

    Worked a treat :)

    Thanks

    Craig

  • Craig100 981 posts 2114 karma points c-trib
    Oct 14, 2015 @ 09:05
    Craig100
    0

    Transferred working code from 7.2.4 to 7.2.8 and get a horrible error when the page reloads after the login button is clicked:

    Exception Details: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

    The following post refers (should have put it in this thread really!): https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/72132-login-partial-view-macro-fail#comment-231619

  • Osman Coskun 145 posts 344 karma points
    Dec 12, 2017 @ 14:37
    Osman Coskun
    0

    Steve's solution works on Umbraco 7.7.7.

    Thank you.

    Edit: After applying Steve's solution, redirect to non-authorized page fails.

    Edit 2: It was my fault. I forgot to remove the login macro from non-authorized page. Every thing works fine.

  • Chris 12 posts 84 karma points
    Apr 09, 2019 @ 15:24
    Chris
    1

    Although I am using a custom login page and controller. I solved this issue after noticing that Umbraco appears to server redirect to the login page; leaving the url in the address bar as the protected page that user wanted to access.

    I ended my login controller with the following to reload the protected page:

    return RedirectToCurrentUmbracoUrl();
    
  • Stefan Turcanu 8 posts 101 karma points
    Apr 11, 2019 @ 08:32
    Stefan Turcanu
    0

    Hi Chris,

    I've been trying to make this work and on the lates version of umbraco it works, but cannot make custom login page & controllers budge none the least. Can you share your approach/code?

    I'm building a site which has a members area with this tree structure

    • home
    • members
      • profile
      • dashboard
    • access denied
    • login

    The idea is if you land on /members, or any child node, you'll get the login page. Following successful login redirect to /dashboard. that works with the built in macro, and as total beginner cannot figure out where to look in the docs on how write the custom controller so i can make use of a way better looking login page :D

Please Sign in or register to post replies

Write your reply to:

Draft