MVC access membership profile info from Action Filter
Hi,
I have an action filter registered, what I want to do in the filter is check if some session variables have been set, if not get the profile info for the currently logged on member and set the session variables.
I don't seem to be able to access the class that handles the member profile, the Action Filter itself runs ok, i've checked by returning some text.
the profile class is pretty standard and just Inherits from ProfileBase.
Does anyone know if is possible to access this class from the filter? I can access other classes I've created eg database access, System.Web.Security.Membership, controllers etc..
Do you just need access to the HttpContext's Profile property ?
You can access the HttpContext in a filter by the ActionExecutingContext like:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Profile...
Also, a controller is an actinon filter itself. You can just override some methods in a controller to get the same result. So you can use the exact same override above inside a controller directly.
Thanks for replying, annoyingly it was to do with my set-up, I had the profile class in the App_Code folder.
I'd added files in there because I used the MVC starter website and that's where a couple of surface controllers are stored, so I thought I'd follow the same convention.
my profile class is located in App_Code, along with some surface controllers and view models. They work fine together, I'd put my Filter in a seperate Filter folder outside of App_Code and it had no visibility to the profile class but did for the MVC controllers and Models. That's the bit that confused me, I guess Visual Studio is applying some rules that apply to non MVC web projects.
I never really got to a great solution for this. How would you go about running some code that checks Session objects and user authentication statuses on every page?
Not sure what you meant by: "I'd put my Filter in a seperate Filter folder outside of App_Code" ?
Only code in the App_Code folder will get compiled, if you just chuck code in folders outside of that folder it won't work unless you compile the code and put the DLL in the /bin
basically I've started off with an empty mvc4 web application, then installed umbraco and some other packages via nuget, so anything i'm adding does get compiled like you say to bin. I've also got a whole load of Entity Framework data models in another project that stores user generated content seperate from Umbraco. I'm using Umbraco mainly for the membership and templates/non user content and mixing in my custom content. At some point i need to add some extra Umbraco back end screens to manage the custom data.
I have to have a facebook login as well as the asp.net membership so the sort of stuff i need to check is whether there's a valid facebook access token and other session objects.
I did try adding a global filter but couldn't get it to work with the global.asax (that's why i got in touch originally :) ), but then did with an app_startup that Structuremap had already added.
At the moment I'm using a surface controller and including the corresponding View as a partial in the main layout view, it doesn't 'seem' like the right way though?
The action in the controller checks the session etc and refreshes if needed.
MVC access membership profile info from Action Filter
Hi,
I have an action filter registered, what I want to do in the filter is check if some session variables have been set, if not get the profile info for the currently logged on member and set the session variables.
I don't seem to be able to access the class that handles the member profile, the Action Filter itself runs ok, i've checked by returning some text.
the profile class is pretty standard and just Inherits from ProfileBase.
Does anyone know if is possible to access this class from the filter? I can access other classes I've created eg database access, System.Web.Security.Membership, controllers etc..
Any help appreciated :)
One thing I was wondering is is I can youse a base controller to do the same thing?
I'm using Surface controllers so I'm not to if i can do that?
Do you just need access to the HttpContext's Profile property ?
You can access the HttpContext in a filter by the ActionExecutingContext like:
Also, a controller is an actinon filter itself. You can just override some methods in a controller to get the same result. So you can use the exact same override above inside a controller directly.
Thanks for replying, annoyingly it was to do with my set-up, I had the profile class in the App_Code folder.
I'd added files in there because I used the MVC starter website and that's where a couple of surface controllers are stored, so I thought I'd follow the same convention.
Cheers
Just a bit more explanation:
my profile class is located in App_Code, along with some surface controllers and view models. They work fine together, I'd put my Filter in a seperate Filter folder outside of App_Code and it had no visibility to the profile class but did for the MVC controllers and Models. That's the bit that confused me, I guess Visual Studio is applying some rules that apply to non MVC web projects.
Hi Shannon,
I never really got to a great solution for this. How would you go about running some code that checks Session objects and user authentication statuses on every page?
Cheers
Matt
Not sure what you meant by: "I'd put my Filter in a seperate Filter folder outside of App_Code" ?
Only code in the App_Code folder will get compiled, if you just chuck code in folders outside of that folder it won't work unless you compile the code and put the DLL in the /bin
Not sure what kind of code you want to run on 'every page' but you could setup an MVC global filter or use route hijacking (http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers)
Thanks Shannon
sorry was a bit hazy with the details there..
basically I've started off with an empty mvc4 web application, then installed umbraco and some other packages via nuget, so anything i'm adding does get compiled like you say to bin. I've also got a whole load of Entity Framework data models in another project that stores user generated content seperate from Umbraco. I'm using Umbraco mainly for the membership and templates/non user content and mixing in my custom content. At some point i need to add some extra Umbraco back end screens to manage the custom data.
I have to have a facebook login as well as the asp.net membership so the sort of stuff i need to check is whether there's a valid facebook access token and other session objects.
I did try adding a global filter but couldn't get it to work with the global.asax (that's why i got in touch originally :) ), but then did with an app_startup that Structuremap had already added.
At the moment I'm using a surface controller and including the corresponding View as a partial in the main layout view, it doesn't 'seem' like the right way though?
The action in the controller checks the session etc and refreshes if needed.
Cheers
Matt
is working on a reply...