Developer forum

Forum » Development » Protect all Pages of an Area

Protect all Pages of an Area

Diogo Lino
Reply

Hello,

 

I'm trying to protect all the pages of an Area, using this code:

            PageCollection areaPages = Dynamicweb.Content.Page.GetPagesByAreaID(areaId);
            foreach (var areaPage in areaPages)
            {
                if (!string.IsNullOrEmpty(passwordTextBox.Text))
                {
                    areaPage.Password = passwordTextBox.Text;
                    areaPage.Protect = true;
                }
                else
                {
                    areaPage.Password = null;
                    areaPage.Protect = false;
                }
                areaPage.Save(false);
            }

But it only takes effect on the frontend after doing an application pool recycle. Am I missing something?
My code also stores the area password on a table in the database and applies to each page on Page.Saved subscriber.

            Dynamicweb.Notifications.Standard.Page.PageNotificationArgs pna = (Dynamicweb.Notifications.Standard.Page.PageNotificationArgs)args;

            DatabaseDataContext dataContext = DatabaseDataContext.GetNewContext();
            string newPassword = dataContext.AreaCustomFields.FirstOrDefault(p => p.AreaId == pna.Target.AreaID).Password;
            if (pna.Target.Password != newPassword && !(pna.Target.Password == null && newPassword == string.Empty))
            {
                if (!string.IsNullOrEmpty(newPassword))
                {
                    pna.Target.Password = newPassword;
                    pna.Target.Protect = true;
                }
                else
                {
                    pna.Target.Password = null;
                    pna.Target.Protect = false;
                }
                pna.Target.Save(false);
            }

Thanks,

Diogo


Replies

 
Morten Bengtson
Reply

Why not use standard permissions on the site?

You could create a special user, only give access to the site for that user and then create a login template where the username is specified in a hidden field, so that the visitor only needs to enter the password.

 
Nuno Aguiar
Reply

 Hi Morten,

 

We need to apply it to all pages of a website, hence the development.

 

I understand using the Intranet/Extranet may be easier, but we have some limitations and need to understand why the development does not work.

 

Could it be an API bug?

 

Best Regards,

 
Morten Bengtson
Reply
This post has been marked as an answer

Hi Nuno,

 

You probably just need to clear the cache.

I think this is what you need to call after all your changes...

Dynamicweb.Content.Page.UpdateFrontendCache();

 

Votes for this answer: 1
 
Diogo Lino
Reply

Hi Morten,

 

I've tried the Dynamicweb.Content.Page.UpdateFrontendCache() with some changes to my code and it worked. :)

 

Thanks,

Diogo

 
Nicolai Høeg Pedersen
Reply

 

You must be logged in to post in the forum