Try our conversational search powered by Generative AI!

Safe to get current page from HttpContext?

Vote:
 

Hi!

We've implemented a custom MembershipProvider and applied an override on the method ValidateUser. ValidateUser takes two arguments, username and password. However inside the method we need to access additional information like EPiServer properties on the current page (which is a login page).

var page = HttpContext.Current.Handler as EPiServer.PageBase;
int variableWithAdditionalInformation = 0;
if (page != null)
{
    // Retrieve value for variableWithAdditionalInformation here
}

What I wondered is if it's safe to assume that HttpContext.Current.Handler always can be cast to EPiServer.PageBase? If not, is there any other way to get hold of CurrentPage inside a MembershipProvider?

Our site is running on EPiServer 7.1 and we're using Web Forms (along with some Web API code).

#140350
Oct 16, 2015 13:03
Vote:
 

Another approach would be to have a property on the start page where you select the login page and then retrieve it by first loading the start page:

var loader = ServiceLocator.Current.GetInstance<IContentLoader>();

var startPage = loader.Get<StartPageData>(ContentReference.StartPage);

var notFoundPage = loader.Get<NotFoundPageData>(startpage.NotFoundPageReference);

 

This approach would work even if the current page isn't the login page, which might happen if someone would end up on the .aspx file directy and the routing can't resolve the correct page.

#140357
Edited, Oct 16, 2015 14:52
Vote:
 

Hmm can't edit my post, notFoundPage should of cource be loginPage and so on...

#140358
Oct 16, 2015 14:53
Vote:
 

Hi Johan,

And thank you for your answer!

I think the solution you suggested would work if we only had one login page. However, we have many login pages so we need to know which one we're currently on.

#140361
Oct 16, 2015 15:28
Vote:
 

How do you decide on which one the user should end up on? Can't you use the same functionality to resolve the login page in you membership provider?

#140362
Oct 16, 2015 15:32
Vote:
 

It's difficult to explain using text but I'll make a try.

Our page tree looks something like this:

Main site

--- Sub site 1

------ Login page 1

--- Sub site 2

------ Login page 2

--- Sub site 3

------ Login page 3

So on sub site 1 we have a link to login page 1, on sub site 2 a link to login page 2 and so on.

I'm just worried that I can't assume this code works all the time:

var page = HttpContext.Current.Handler as EPiServer.PageBase;

I've seen examples with MembershipProviders where the ValidateUser-method is set to take three or four arguments but I'd rather avoid going this way if I can.

#140370
Oct 16, 2015 16:26
Vote:
 

Use the same logic to find the sub site and then fall back on the start page. Then you know for sure that you have a PageData object. The handler could be something else if you're not routed correctly. Or use HttpContext.Current.Handler as EPiServer.PageBase and if that's result is null you can fall back on the start page.

#140371
Oct 16, 2015 16:32
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.