Allow guests to view homepage only

5 years ago
I'm trying to find a solution to allow users to only be able to view the homepage, but when they click on anything it re-directs to the register page.  I tried playing around with the access control list, but if you turn navigation off for guests they can't even view the homepage.  

Any ideas would be much appreciated.
5 years ago
finalthrill wrote:
I'm trying to find a solution to allow users to only be able to view the homepage, but when they click on anything it re-directs to the register page.  I tried playing around with the access control list, but if you turn navigation off for guests they can't even view the homepage.  

Any ideas would be much appreciated.


One very easy solution is to:

1) Open Root.Head.cshtml
2) Add the following at the very top (after using statements)

@* other using statements ... *@
using Nop.Core.Domain.Customers;

@{
    if (WorkContext.CurrentCustomer.IsGuest() &&
        Request.Url.AbsolutePath != "/" &&
        Request.Url.AbsolutePath != "login") // and other pages you want to allow
    {
        Response.Redirect("~/", true);
    }
}


Not the most sophisticated solution, but it's quick and it works! :)
5 years ago
Thank you wooncherk.  I'm not sure how you got that code to work as it creates an infinite re-direct loop for me.
5 years ago
finalthrill wrote:
Thank you wooncherk.  I'm not sure how you got that code to work as it creates an infinite re-direct loop for me.


Can you check the updated code? I updated 1 minute after my original post. :)
5 years ago
Nevermind, your edit fixed it.  Thank you for the simple solution!
5 years ago
finalthrill wrote:
Nevermind, your edit fixed it.  Thank you for the simple solution!


Make sure to check all the necessary pages (register, contact us and etc). Mine is just a simplified version. :)
5 years ago
I just had to modify it a bit to make sure it hits a topic page anywhere they try to go anywhere other than the homepage and login page:

@{
    if (WorkContext.CurrentCustomer.IsGuest() &&
        Request.Url.AbsolutePath != "/" &&
        Request.Url.AbsolutePath != "/t/PageNotFound" &&
        Request.Url.AbsolutePath != "/login")
    {
        Response.Redirect("/t/PageNotFound", true);
    }
}

Works like a charm!  Thanks again!
5 years ago
finalthrill wrote:
I just had to modify it a bit to make sure it hits a topic page anywhere they try to go anywhere other than the homepage and login page:

@{
    if (WorkContext.CurrentCustomer.IsGuest() &&
        Request.Url.AbsolutePath != "/" &&
        Request.Url.AbsolutePath != "/t/PageNotFound" &&
        Request.Url.AbsolutePath != "/login")
    {
        Response.Redirect("/t/PageNotFound", true);
    }
}

Works like a charm!  Thanks again!


Forgot to mention, there can be a bug in my earlier code due to string comparison being case-sensitive. So we need to make sure we ignore the case. A better version is to use:

Request.Url.AbsolutePath.Equals("/login", StringComparison.InvariantCultureIgnoreCase)
5 years ago
Good catch!  I just had to add not conditions to each inequality:  


@{
    if (WorkContext.CurrentCustomer.IsGuest() &&
        !(Request.Url.AbsolutePath.Equals("/", StringComparison.InvariantCultureIgnoreCase)) &&
        !(Request.Url.AbsolutePath.Equals("/t/PageNotFound", StringComparison.InvariantCultureIgnoreCase)) &&
        !(Request.Url.AbsolutePath.Equals("/login", StringComparison.InvariantCultureIgnoreCase)))// and other pages you want to allow
    {
        Response.Redirect("/t/PageNotFound", true);
    }
}
5 years ago
finalthrill wrote:
Good catch!  I just had to add not conditions to each inequality:  


@{
    if (WorkContext.CurrentCustomer.IsGuest() &&
        !(Request.Url.AbsolutePath.Equals("/", StringComparison.InvariantCultureIgnoreCase)) &&
        !(Request.Url.AbsolutePath.Equals("/t/PageNotFound", StringComparison.InvariantCultureIgnoreCase)) &&
        !(Request.Url.AbsolutePath.Equals("/login", StringComparison.InvariantCultureIgnoreCase)))// and other pages you want to allow
    {
        Response.Redirect("/t/PageNotFound", true);
    }
}


Yeap! Sounds good! :)