I've re-worded this to try and get a solution.
I'm using BlogEngine.NET 3.3. I have a requirement to show 300 Characters of the posts in the blog and then the registered user will then click the post name to read the rest.
I would like to un-registered users (Anonymous users) to be able to see the 300 characters but when they try to read the full content of the post they get some text saying "Please Register to see this content".
I've scoured the net trying to find out if someone has achieved this before. I found the below code. It says to put it into the App_Code/Extensions folder as a .cs to enable it. However, in 3.3 there isn't an extensions folder in App_Code. There is one here in here BlogEngine.Core\Web\Extensions. I've tried putting the below code into the web\extensions folder and it appears to do something. It hides all of my published posts.
Could someone please help me with this?
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;
using System.Collections.Generic;
/// <summary>
/// Summary description for PostSecurity
/// </summary>
[Extension("Checks to see if a user can see this blog post.",
"1.0", "<a href=\"http://www.lavablast.com\">LavaBlast.com</a>")]
public class PostSecurity
{
static protected ExtensionSettings settings = null;
public PostSecurity()
{
Post.Serving += new EventHandler<ServingEventArgs>(Post_Serving);
ExtensionSettings s = new ExtensionSettings("PostSecurity");
s.AddParameter("Role", "Role", 50, true);
s.AddParameter("Category", "Category", 50);
// describe specific rules for entering parameters
s.Help = "Checks to see if the user has any of those roles before displaying the post. ";
s.Help += "You can associate a role with a specific category. ";
s.Help += "All posts having this category will require that the user have the role. ";
s.Help += "A parameter with only a role without a category will enable to filter all posts to this role. ";
s.AddValues(new string[] { "Registered", "" });
ExtensionManager.ImportSettings(s);
settings = ExtensionManager.GetSettings("PostSecurity");
}
protected void Post_Serving(object sender, ServingEventArgs e)
{
Post post = (Post)sender;
bool continu = false;
MembershipUser user = Membership.GetUser();
continu = user != null;
if (user != null)
{
List<string> categories = new List<string>();
foreach (Category cat in post.Categories)
categories.Add(cat.Title);
string[] r = Roles.GetRolesForUser();
List<string> roles = new List<string>(r);
DataTable table = settings.GetDataTable();
foreach (DataRow row in table.Rows)
{
if (string.IsNullOrEmpty((string)row["Category"]))
continu &= roles.Contains((string)row["Role"]);
else
{
if (categories.Contains((string)row["Category"]))
continu &= roles.Contains((string)row["Role"]);
}
}
}
e.Cancel = !continu;
}
}
This has now been resolved. rtur from BlogEngine.Net kindly assisted with this.
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;
using System.Web;
[Extension("Secure post", "1.0", "BlogEngine.NET")]
public class SecurePost
{
static SecurePost()
{
Post.Serving += Post_Serving;
}
private static void Post_Serving(object sender, ServingEventArgs e)
{
if(e.Location == ServingLocation.SinglePost)
{
if (!HttpContext.Current.User.Identity.IsAuthenticated)
{
HttpContext.Current.Response.Redirect("~/account /login.aspx");
}
}
}
}