Is there a place where I can execute this session code for each request in nopCommerce?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 年 前
I am looking to implement some business logic in my store as follows:

if (User.Identity.IsAuthenticated)
            {

            }
            else
            {
                if (System.Web.HttpContext.Current.Session["SecurityCodeApproved"] == null || !(bool)System.Web.HttpContext.Current.Session["SecurityCodeApproved"])
                {
                    Response.RedirectToRoute("Nop.Plugin.EVO.MyCustomLogin");
                }

            }

I need to execute some code for every controller request.
Is there a place where I can execute this session code for each request and if the session is null redirect them to MyCustomLogin page where they will be able to reenter the session code again.
11 年 前
arlen_bs wrote:
I am looking to implement some business logic in my store as follows:

if (User.Identity.IsAuthenticated)
            {

            }
            else
            {
                if (System.Web.HttpContext.Current.Session["SecurityCodeApproved"] == null || !(bool)System.Web.HttpContext.Current.Session["SecurityCodeApproved"])
                {
                    Response.RedirectToRoute("Nop.Plugin.EVO.MyCustomLogin");
                }

            }

I need to execute some code for every controller request.
Is there a place where I can execute this session code for each request and if the session is null redirect them to MyCustomLogin page where they will be able to reenter the session code again.


Use an MVC Filter. I have used Filter a lot in my plugins, and they are very very handy! :)
11 年 前
The Session won't be available in ApplicationBeginRequest but it might be in AuthenticateRequest.

Global.asax.cs -> Application_AuthenticateRequest

You could also override OnActionExecuting in BaseNopController.
11 年 前
arlen_bs wrote:
I am looking to implement some business logic in my store as follows:

if (User.Identity.IsAuthenticated)
            {

            }
            else
            {
                if (System.Web.HttpContext.Current.Session["SecurityCodeApproved"] == null || !(bool)System.Web.HttpContext.Current.Session["SecurityCodeApproved"])
                {
                    Response.RedirectToRoute("Nop.Plugin.EVO.MyCustomLogin");
                }

            }

I need to execute some code for every controller request.
Is there a place where I can execute this session code for each request and if the session is null redirect them to MyCustomLogin page where they will be able to reenter the session code again.


Continuing with my suggestion of using an MVC Filter, the fact that you are using a plugin means you can put the MVC Filter inside your plugin! No need for any change of the core code. Very sleek. :)
11 年 前
I did but another error : "Object reference not set to an instance of an object."  

protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            //we don't do it in Application_BeginRequest because a user is not authenticated yet
            SetWorkingCulture();
            if (User.Identity.IsAuthenticated)
            {

            }
            else
            {
                if (System.Web.HttpContext.Current.Session["ApexSecurityCodeApproved"] == null || !(bool)System.Web.HttpContext.Current.Session["ApexSecurityCodeApproved"])
                {
                    Response.RedirectToRoute("Nop.Plugin.EVO.APEXLogin");
                }

            }

        }
11 年 前
How to use filters for my purpose inside the plugin?
I've never done that before could you please provide me with more information or any samples?
11 年 前
arlen_bs wrote:
How to use filters for my purpose inside the plugin?
I've never done that before could you please provide me with more information or any samples?


Try this post, I have done some pretty detailed explanation over here: https://www.nopcommerce.com/boards/t/19724/how-to-load-cshtml-file-from-plugin.aspx
11 年 前
But I still don't know how to reach my goal.
11 年 前
arlen_bs wrote:
But I still don't know how to reach my goal.


You need to look up how to write an MVC Filter, and it's widely available on the Internet. Then, override the OnActionExecuting method, which you can put in your logic like


IWorkContext workContext = EngineContext.Current.Resolve<IWorkContext>();

if (!workContext.CurrentCustomer.IsGuest())
{}
else
{}
11 年 前
I added this section in BaseNopController.

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.User.Identity.IsAuthenticated)
            {

            }
            else
            {

                if (System.Web.HttpContext.Current.Session["ApexSecurityCodeApproved"] == null || !(bool)System.Web.HttpContext.Current.Session["ApexSecurityCodeApproved"])
                {
                    //RedirectToRoute("Nop.Plugin.EVO.APEXLogin");
                }
            }

            /// Save information to the database
        }



but the RedirectToRoute is not working.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.