Customer controller redirect based on role

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
below code based on role is not working always goes to else condition
How to use role name at Customer Controller

                            if (string.IsNullOrEmpty(returnUrl) || !Url.IsLocalUrl(returnUrl))
                            {
                                if (EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("LoggedInUserGold") || _workContext.CurrentCustomer.IsInCustomerRole("LoggedInUserGold"))
                                {
                                    return RedirectToRoute("LoggedInUserGoldCtr");
                                }
                                else
                                {
                                    return RedirectToRoute("Home");

                                }                                                        
                            
                            }
5 years ago
Hello,

There are a couple of possibilities why your code isn't working the way you want it to.

Firstly, the first if statement might not be correct. If you have a returnUrl parameter in your URL this will be false.

After that, your second if statement has 2 boolean operations that are essentially the same. The "EngineContext.Current.Resolve<IWorkContext>" is resolving the IWorkContext for you but I can see you already have it in the _workContext variable. I would suggest you write the if statement like:

if(_workContext.CurrentCustomer.IsInCustomerRole("LoggedInUserGold")) 
{
     ....
}


The string passed to the "IsInCustomerRole()" method should be exactly the same as your customer role name. If that is not correct your check won't return true. You should check that.

Hope that helps!

Regards,
Anton
5 years ago
The bottom line is that you should debug/step through your code.

(Why do you have both
EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("LoggedInUserGold")
||
_workContext.CurrentCustomer.IsInCustomerRole("LoggedInUserGold")
?
It seems redundant;  the _workContext should be accurate ... no need to do another Resolve )
5 years ago
The bottom line is that you should debug/step through your code.

(Why do you have both
EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("LoggedInUserGold")
||
_workContext.CurrentCustomer.IsInCustomerRole("LoggedInUserGold")
?
It seems redundant;  the _workContext should be accurate ... no need to do another Resolve )
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.