Password protect posts and pages in BlogEngine

mb_0024_lock_thumb.pngSometimes you might want to protect individual posts or pages with a password so that only users you sent password to can access this resource. Not exactly wide-spread scenario, which explains why it is not currently supported by BlogEngine. But when you need it you need it, and this extension should cover the basics.

Installing and using extension

You can download extension from the gallery and install it following general instructions. To use, when creating or updating post or page add this anywhere inside body: [ password:mypassword ]. There is no gaps inside square brackets, and you’ll use your own password instead of “mypassowrd”.

Password protect posts

The way it works for posts, it only protects single post, or post details, the one with comment form on it. So you would need to use it with [ more ] tag to only show a teaser or whatever best in your case. Then when someone clicks “more” extension will redirect to the page where user can enter password.

image_thumb_20.png

Password protect pages

For pages it is easier as they not listed and page menu goes directly to the page which is protected. One thing to remember is that most likely you want to uncheck “show in list” for protected pages, so they won’t even show up in the menu in the first place. Then of cause you need to provide link to the page along with password to authorized user(s).

image_thumb_18.png

For authenticated users only?

Also, if you wish authenticated users only were able to access the page, replace code in ~/App\_Code/Extensions/PasswordProtect.cs with code below and use [ authenticated ] instead of [ password:thepassword ] when create/update post or page.

    namespace App_Code.Extensions
    {
        using System;
        using BlogEngine.Core;
        using BlogEngine.Core.Web.Controls;
        using System.Web;
    
        [Extension("Password protect posts and pages.", "1.0", "<a href=\"http://rtur.net/blog\">rtur.net</a>")]
        public class PasswordProtected
        {
            public PasswordProtected()
            {
                Post.Serving += Serving;
                BlogEngine.Core.Page.Serving += Serving;
            }
    
            private static void Serving(object sender, ServingEventArgs e)
            {
                if (e.Location == ServingLocation.SinglePost || e.Location == ServingLocation.SinglePage)
                {
                    if(e.Body.Contains("[authenticated]"))
                    {
                        e.Body = e.Body.Replace("[authenticated]", "");
                        if (!HttpContext.Current.User.Identity.IsAuthenticated)
                        {
                            HttpContext.Current.Response.Redirect(string.Format("{0}Account/login.aspx", Utils.RelativeWebRoot));
                        }
                    }
                }
            }
        }
    }

Download Password Protect

About RTUR.NET

This site is all about developing web applications with focus on designing and building open source blogging solutions. Technologies include ASP.NET Core, C#, Angular, JavaScript and more.