Only allow logged in users to see blog posts

Topics: Controls
Feb 2, 2010 at 11:55 AM

Hi,

 

Is there a way to restrict access to blogengine only to users login?

Basically I wanted to prevent people from viewing blog entries unless

they are logged in? I tried disabling the publish checkbox but this then

prevents posts from being archived?

 

Thanks in advance,

joe

Feb 13, 2010 at 8:18 PM

BlogEngine does not provide this functionality.

However, it is trivial to implement. Simply add an extension that replaces the post content with an empty string (or message) if the user is not authenticated. 

If you want to do this for certain posts, just embed a token in posts that you want to conceal, and have your extension search for the token.

Here's a simple example which does the same for comments. You'd need to hook the Post.Serving event instead.

 

#region using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text.RegularExpressions;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;
using System.Xml.Linq;
using CalvaryGrace.Core.Common.Utilities;
using CalvaryGrace.Core.Common;
using CalvaryGrace.WebControls;
using System.Text;
using System.Collections.Specialized;
#endregion
/// <summary>
/// Summary description for CommentVeiler
/// </summary>
[Extension("Hides comments from any non-members", "1.0", "Michael Jones")]
public class CommentVeiler
{
   static protected ExtensionSettings _settings = null;
   public CommentVeiler()
   {
      Comment.Serving += new EventHandler<ServingEventArgs>(Comment_Serving);
      ExtensionSettings settings = new ExtensionSettings("CommentVeiler");
      settings.IsScalar = true;
      settings.AddParameter("HiddenCommentText", "Hidden Comment Text", 100, true);
      settings.AddValue("HiddenCommentText", "This comment is visible to members only.");
      _settings = ExtensionManager.InitSettings("CommentVeiler", settings);
   }
   /// <summary>
   /// Called when a comment is served. If the current context indicates that the
   /// user is not authorized/logged into the site, the comment will be hidden.
   /// </summary>
   /// <param name="sender"></param>
   /// <param name="e"></param>
   void Comment_Serving(object sender, ServingEventArgs e)
   {
      if (false == System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
      {
         e.Body = _settings.GetSingleValue("HiddenCommentText");
      }
   }
}

 

 

#region using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text.RegularExpressions;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;
using System.Xml.Linq;
using System.Text;
using System.Collections.Specialized;

#endregion

/// <summary>
/// Summary description for CommentVeiler
/// </summary>
[Extension("Hides comments from any non-members", "1.0", "My Name")]
public class CommentVeiler
{
   static protected ExtensionSettings _settings = null;

   public CommentVeiler()
   {
      Comment.Serving += new EventHandler<ServingEventArgs>(Comment_Serving);

      ExtensionSettings settings = new ExtensionSettings("CommentVeiler");

      settings.IsScalar = true;

      settings.AddParameter("HiddenCommentText", "Hidden Comment Text", 100, true);
      settings.AddValue("HiddenCommentText", "This comment is visible to members only.");

      _settings = ExtensionManager.InitSettings("CommentVeiler", settings);
   }

   /// <summary>
   /// Called when a comment is served. If the current context indicates that the
   /// user is not authorized/logged into the site, the comment will be hidden.
   /// </summary>
   /// <param name="sender"></param>
   /// <param name="e"></param>
   void Comment_Serving(object sender, ServingEventArgs e)
   {
      if (false == System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
      {
         e.Body = _settings.GetSingleValue("HiddenCommentText");
      }
   }
}