BlogEngine.NET 3.3 - 防止匿名用户做某些事情

时间:2016-05-12 19:17:38

标签: blogengine.net

我已经重新措辞,试图找到解决方案。

我正在使用BlogEngine.NET 3.3。我要求在博客中显示300个字符的帖子,然后注册用户将点击帖子名称以阅读其余内容。

我想取消注册用户(匿名用户)以便能够看到300个字符,但当他们尝试阅读帖子的全部内容时,他们会收到一些文字说明"请注册以查看此内容& #34;

我已经在网上搜寻,试图找出有人之前是否实现了这一目标。我找到了下面的代码。它说它将它作为.cs放入App_Code / Extensions文件夹中以启用它。但是,在3.3中,App_Code中没有扩展文件夹。这里有一个BlogEngine.Core \ Web \ Extensions。我已经尝试将以下代码放入web \ extensions文件夹中,它似乎可以执行某些操作。它隐藏了我发布的所有帖子。

有人可以帮帮我吗?

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;

   }

}

2 个答案:

答案 0 :(得分:0)

好的,所以前段时间我使用了BlogEngine.Net,我会尽力帮助你,所以我不确定我的答案是否正确,但也许它会给你一些指示,好吗?

您不应该授予会员查看未发布帖子的访问权限,因为这对于网站上的编辑来说更是如此,以便能够在发布新帖子之前保存新帖子以供公众使用。

根据我的理解(?),只有你的朋友会在博客上写帖子,因此他应该是唯一获得该许可的人。

可能有用的一件事是,每个人都有权观看帖子,如果需要让第一页工作(我真的不记得)。然后,您可以覆盖/自定义显示帖子的控件/视图,在那里您可以检查用户是否实际注册并决定显示帖子或消息告诉他们注册。

答案 1 :(得分:0)

现在已经解决了。来自BlogEngine.Net的rtur对此有所帮助。

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");
        }
    }
  }
}