For blocking the catalog to unregistered users I have implemented the following code. (Theres alot of code before the bug)
In the Index Home Controller I have added the following.
using System;
using System.Linq;
using System.Web.Mvc;
using System.Collections.Generic;
using Nop.Core;
using Nop.Core.Infrastructure;
using Nop.Core.Domain.Customers;
using Nop.Services.Authentication;
using Nop.Services.Authentication.External;
using Nop.Services.Localization;
using Nop.Services.Customers;
using Nop.Web.Framework.Security;
using Nop.Web.Models.Customer;
using Nop.Core.Domain.Localization;
using Nop.Services.Messages;
using Nop.Services.Helpers;
using Nop.Core.Domain.Tax;
using Nop.Services.Tax;
using Nop.Services.Common;
using Nop.Services.Directory;
using Nop.Core.Domain.Common;
using Nop.Core.Domain.Messages;
using Nop.Web.Models.Common;
namespace Nop.Web.Controllers
{
public partial class HomeController : BaseNopController
{
#region Routes
private class AllowedRoutes
{
public string Controller { get; set; }
public string Action { get; set; }
}
protected override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
var currentAction = filterContext.RouteData.Values["action"].ToString();
var currentController = filterContext.RouteData.Values["controller"].ToString();
var allowedActions = new List<AllowedRoutes>
{
new AllowedRoutes {Controller = "Home", Action = "Index"},
new AllowedRoutes {Controller = "Customer", Action = "Login"},
new AllowedRoutes {Controller = "Customer", Action = "Register"},
new AllowedRoutes {Controller = "Topic", Action = "TopicDetails"},
new AllowedRoutes {Controller = "HomePageNews", Action = "News"},
new AllowedRoutes {Controller = "Common", Action = "contactus"}
};
var query = from a in allowedActions
where a.Action == currentAction &&
a.Controller == currentController
select a;
var ignorePage = (query.ToList().Count > 0);
var workContext = EngineContext.Current.Resolve<IWorkContext>();
}
#endregion
#region Feilds
private readonly CustomerSettings _customerSettings;
private readonly ICustomerRegistrationService _customerRegistrationService;
private readonly ICustomerService _customerService;
private readonly IWorkContext _workContext;
private readonly IAuthenticationService _authenticationService;
private readonly ILocalizationService _localizationService;
#endregion
#region ctor
public HomeController
(
CustomerSettings customerSetting,
ICustomerRegistrationService customerRegistrationService,
ICustomerService customerService,
IWorkContext workContext,
IAuthenticationService authenticationService,
ILocalizationService localizationService
)
{
this._customerSettings = customerSetting;
this._customerRegistrationService = customerRegistrationService;
this._customerService = customerService;
this._workContext = workContext;
this._authenticationService = authenticationService;
this._localizationService = localizationService;
}
#endregion
//Richard Evans
//31/08/2012 - Description == Login Authentication For Registered Users.
//Start
#region Login
public ActionResult Index()
{
return View();
}
[NopHttpsRequirement(SslRequirement.Yes)]
public ActionResult Login(bool? checkoutAsGuest)
{
{
var model = new LoginModel();
model.UsernamesEnabled = _customerSettings.UsernamesEnabled;
model.CheckoutAsGuest = model.CheckoutAsGuest = checkoutAsGuest.HasValue ? checkoutAsGuest.Value : false;
return View(model);
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(LoginModel model, string returnUrl, bool? checkoutAsGuest)
{
if (ModelState.IsValid)
{
if (_customerSettings.UsernamesEnabled && model.Username != null)
{
model.Username = model.Username.Trim();
}
if (_customerRegistrationService.ValidateCustomer(_customerSettings.UsernamesEnabled ? model.Username : model.Email, model.Password))
{
var customer = _customerSettings.UsernamesEnabled ? _customerService.GetCustomerByUsername(model.Username) : _customerService.GetCustomerByEmail(model.Email);
//sign in new customer
_authenticationService.SignIn(customer, model.RememberMe);
//Richard 29/08/2012 -- Description --
//Added ReturnRedirect To Url to remain on the Home Index page.
if (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
return Redirect(returnUrl);
else
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", _localizationService.GetResource("Account.Login.WrongCredentials"));
}
}
//If we got this far, something failed, redisplay form
model.UsernamesEnabled = _customerSettings.UsernamesEnabled;
return View(model);
}
public ActionResult LoginNavigation(LoginModel model)
{
return PartialView(model);
}
#endregion
//Finsh
}
}
I have wrote the following Partial View,
@model Nop.Web.Models.Customer.LoginModel
<div class="block block-login-navigation">
<div class="message-error">
@Html.ValidationSummary(false, T("Account.Login.Unsuccessful").Text)
</div>
<div class="title">
@T("Login")
</div>
<div class ="listbox">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken();
<div class="listbox">
<ul class="row">
<li class="username;">
@Html.LabelFor(m => m.Username):
</li>
<li class="inputlogin;">
@Html.TextBoxFor(m => m.Username)
@Html.ValidationMessageFor(m => m.Username)
</li>
</ul>
<ul class="row">
<li class="item-name">
@Html.LabelFor(m => m.Password):
</li>
<li class="item-value">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</li>
</ul>
<ul class="row">
<li class="item-value">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</li>
</ul>
<ul class="row">
<li class="forgot-password">
@Html.RouteLink(T("Account.Login.ForgotPassword").Text, "PasswordRecovery")
</li>
</ul>
<ul class="row">
<li>
<button class="loginbutton" type="submit">@T("Account.Login.LoginButton")</button>
</li>
</ul>
</div>
}
</div>
</div>
Using the shared Columns folder I have wrote the following the contians an
HTML.Action -
@*Description - Login Navigation Column For User Authentication.*@
@*Richard Evans - 25/06/2012*@
@{
Layout = "~/Views/Shared/_Root.cshtml";
}
<div class="master-wrapper-leftside-3">
@if (IsSectionDefined("left"))
{
@RenderSection("left")
}
else
{
@Html.Action("WidgetsByZone", "Widget", new { widgetZone = Nop.Core.Domain.Cms.WidgetZone.BeforeLeftSideColumn })
<div class="clear">
</div>
@Html.Action("LoginNavigation", "Home")
<div class="clear">
</div>
@Html.Action("RegisterNavigation", "Customer")
<div class="clear">
</div>
@Html.Action("WidgetsByZone", "Widget", new { widgetZone = Nop.Core.Domain.Cms.WidgetZone.AfterLeftSideColumn })
}
</div>
<div class="master-wrapper-center-3">
<div class="master-wrapper-cph-3">
@Html.Action("WidgetsByZone", "Widget", new { widgetZone = Nop.Core.Domain.Cms.WidgetZone.BeforeMainColumn })
@RenderBody()
@Html.Action("WidgetsByZone", "Widget", new { widgetZone = Nop.Core.Domain.Cms.WidgetZone.AfterMainColumn })
</div>
</div>
<div class="master-wrapper-rightside-3">
@if (IsSectionDefined("right"))
{
@RenderSection("right")
}
else
{
@Html.Action("WidgetsByZone", "Widget", new { widgetZone = Nop.Core.Domain.Cms.WidgetZone.BeforeRightSideColumn })
<div class="clear">
</div>
@Html.Action("ConcordSpecialOffers", "Catalog")
<div class="clear">
</div>
@Html.Action("WidgetsByZone", "Widget", new { widgetZone = Nop.Core.Domain.Cms.WidgetZone.AfterRightSideColumn })
}
</div>
Okay the problem???
As you can see I have allowed the following routes
new AllowedRoutes {Controller = "Home", Action = "Index"},
new AllowedRoutes {Controller = "Customer", Action = "Login"},
new AllowedRoutes {Controller = "Customer", Action = "Register"},
new AllowedRoutes {Controller = "Topic", Action = "TopicDetails"},
new AllowedRoutes {Controller = "HomePageNews", Action = "News"},
new AllowedRoutes {Controller = "Common", Action = "contactus"}
On excuting the partial view on the Home Index view/page I can access the catalog on my development server / Local IIS server and the deadicated server.
I have this code Implemented in the Index View Page so the login navigation Shared view is there for the user before accessing columnsThree.
@if (Request.IsAuthenticated)
{
Layout = "~/Views/Shared/_ColumnsThree.cshtml";
}
else
{
Layout = "~/Views/Shared/_ColumnsConcord.cshtml";
@Html.AntiForgeryToken()
}
If I navigated to the Topic Details -- say for example Shipping Returns / Systemname I have same peice of code as above
@if (Request.IsAuthenticated)
{
Layout = "~/Views/Shared/_ColumnsThree.cshtml";
}
else
{
Layout = "~/Views/Shared/_ColumnsConcord.cshtml";
}
It will not allow me to login.
I have noticed the Url from the Local Development and IIS routes to following.
http://localhost:57436/t/shippinginfocode]
[code]t/shippinginfo
With this in Mind do I need to make any alteration's to the new allowed routing structure in the Index Controller or I'm I missing something in the code. To allow my partial view to be excuted over the Topic Details/Block - So the unregistered user can read the information etc etc - -
If you could help It would be greatly apped
Kind Regards
Richard.