Copied to clipboard

Flag this post as spam?

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


  • Calvin Frei 106 posts 314 karma points
    Jan 11, 2016 @ 11:02
    Calvin Frei
    0

    CurrentCustomer IsAnonymous when Customer is logged in

    Hi

    I have following setup:

    • Umbraco 7.3.4
    • Merchello 1.13.3
    • Customer as default member type
    • /account url (protected with member type 'Customer')

    The problem is:
    Member logs in and adds something to the basket. Then he closes the browser. After some minutes he goes directly to /account but then an exception throws because it cannot cast CustomerContext to ICustomer (when debugging: Request.IsAuthenticated = true, CustomerContext.IsAnonymous = true).

    An other problem is, that the basket is also empty (until you do a redirect).

    Has it do something with a cookie? Is there an extra cookie stored for the ICustomer? How long is this cookie stored? E.g. I want to keep the login for 3 months.

    To reproduce in Visual Studio: Just stop the solution after login and then set /account as the default start page and then start the solution again.

    You can "solve" the problem when you do a redirect, then it seems to work. But is that really the solution?

    The AccountController looks like this:

    [Authorize]
    public class AccountController : MerchelloRenderMvcController
    {
    
        public override ActionResult Index(RenderModel model)
        {   
            ProfileRenderModel viewModel = new ProfileRenderModel(model.Content, model.CurrentCulture)
            {
                Profile = new ProfileModel(
                customer: base.CurrentCustomer as ICustomer) // Here it throws the exception
            };
    
            return this.View(model: viewModel);
        }
    }
    

    Thank you
    Calvin

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Jan 11, 2016 @ 16:50
    Rusty Swayne
    0

    Hey Calvin,

    My guess is the reason this is happening, is the CustomerContext is getting set to an anonmous customer on the initial page load and then does the conversion to an ICustomer on the subsequent page load.

    Try using the MerchelloViewPage{T} in the initial page load template.

  • Calvin Frei 106 posts 314 karma points
    Jan 12, 2016 @ 08:11
    Calvin Frei
    0

    Hey Rusty

    Thank you for your answer. This seems to work... I have to change some Views/Controllers and then I let you know if it really works but It's still not the nicest solution, IMO :). At least there is no extra redirect.

    The problem with the basket is still there. For the first time you access the page the TotalQuantityCount is zero until you do a redirect.

    My controller looks like this:

    [PluginController("v1")]
    public class BasketSurfaceController : MerchelloSurfaceController
    {
        [ChildActionOnly]
        public ContentResult TotalQuantityCount()
        {
            ContentResult result = new ContentResult();
            result.Content = (base.Basket.IsEmpty() ? 0 : base.Basket.TotalQuantityCount).ToString();
            return result;
        }
    }   
    

    And that's how I use it in my View:

    @inherits Merchello.Web.Mvc.MerchelloTemplatePage
    
    <ul class="nav navbar-nav">
        <li>
            <a href="/basket">
                Basket
                (@(Html.Action<BasketSurfaceController>("TotalQuantityCount")))
            </a>
        </li>
    </ul>
    

    Thank you
    Calvin

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Jan 12, 2016 @ 16:21
    Rusty Swayne
    0

    It's probably the second request (your redirect) that is getting things to kick - and may indicate that the request caching is a bit aggressive in the CustomerContextBase for your scenario.

    You might also verify that your Umbraco member is returning authenticated on the first request.

    Here is the CustomerContextBase code if you want to give it a look and see if you can find a culprit there ...

    https://github.com/Merchello/Merchello/blob/merchello-dev/src/Merchello.Web/Pluggable/CustomerContextBase.cs#L243

  • Calvin Frei 106 posts 314 karma points
    Jan 19, 2016 @ 14:33
    Calvin Frei
    0

    Hi Rusty

    Sorry for my late reply. I now have changed my Controllers to achieve that the CustomerContext is not Anonymous when trying to access /account directly.

    The problem is that I have to call the CustomerContext twice otherwise it won't be an ICustomer.

    So I had to implement it like this:

    Account.cshtml (this is the page which gets called when accessing /account)

    @inherits Merchello.Web.Mvc.MerchelloTemplatePage
    
    // First call of CustomerContext
    @(Html.Action("Profile", "AccountSurface", new { customer = base.CurrentCustomer }))
    

    And then in the SurfaceController

    [Authorize, ChildActionOnly]
    public PartialViewResult Profile(ICustomerBase customer)
    {
        // Here it is Anonymous but when calling it again it is an ICustomer
        if (customer.IsAnonymous)
        {
            // Always here after browser is closed
            customer = base.CurrentCustomer;
        }
    
        ProfileModel model = new ProfileModel(
            customer: customer as ICustomer);
    
        return this.PartialView(
            viewName: "Account/Profile",
            model: model);
    }
    

    So maybe as you said I have to look at the CustomerContext implementation :-).

    Calvin

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Jan 19, 2016 @ 16:46
Please Sign in or register to post replies

Write your reply to:

Draft