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?
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:
@* 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>
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).
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.
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?
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.
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.
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.
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:
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
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?
I have used this gist and it works like you want :
https://gist.github.com/anonymous/4336141
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
You made my day, thanks!
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>
}
}
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).
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. :)
That's an interesting find - I'll put a note of warning on my blog post. Thanks!
I just came across the same. Upgrading to 7.1.8 (from 7.1.4) solved the homepage redirect issue.
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?
I've the same problem. This is a big issue.
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:
Add this in the
@using BeginUmbracoForm
block: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.
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.
Love it - so easy. Thanks John.
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.
this solve my problem :
Response.Redirect(Request.RawUrl);
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 😡
So, I've to create a controller in app_code. Why Do not work the default?
it works in my case, im not sure why it doesnt work at yours!
Do work the default 4 you?
No
I think that we need an official doc and report for this. Is there s bug open?
I solved.
@Steve Morgan, Excellent tutorial.
Worked a treat :)
Thanks
Craig
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:
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
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.
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:
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
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
is working on a reply...