Prevent non-members from viewing entire post?

Topics: Business Logic Layer, Controls
Aug 19, 2010 at 9:16 PM

I added code to the post.aspx page to prevent anonymous visitors from reading a post.  However what I would really like to do is allow them to view a preview of the post if they are not logged in and the entire post when they do log in.

I don't understand the way BlogEngine.net works well enough to feel confident on doing this myself without breaking it.  Can someone point me in the right direction to intercept the blog post and truncate it at a certain point (or when [more] is encountered)?

 

Thanks!

Coordinator
Aug 20, 2010 at 10:47 AM

Assuming you're using [more] in your posts (which it sounds like you are), you can modify the BreakPost extension to always output [more] instead of showing the entire post for anonymous visitors.  In the App_Code\Extensions folder is BreakPost.cs.  In the Post_Serving event is this:

if (e.Location == ServingLocation.PostList)

Change that to:

if (e.Location == ServingLocation.PostList ||
	(System.Threading.Thread.CurrentPrincipal != null &&
	 !System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated))
{
	AddMoreLink(sender, e);
}

That's all you need to do.

You could then customize this further inside AddMoreLink(), so it maybe displays "login to read more" instead of "More..." for the anonymous visitors.  Where "Resources.labels.more" is, that could be conditionally changed to "login to read more" by checking if they are logged in with the same type of code above.  I.E.

if (System.Threading.Thread.CurrentPrincipal != null &&
	!System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
{
	// login to read more
}
else
{
	// Resources.labels.more
}

Aug 20, 2010 at 11:06 PM

Excellent.  Thank you.