【问题标题】:BlogEngine.NET 3.3 - Prevent anonymous users from doing certain thingsBlogEngine.NET 3.3 - 防止匿名用户做某些事情
【发布时间】:2016-05-12 19:17:38
【问题描述】:

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

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

我希望未注册用户(匿名用户)能够看到这 300 个字符,但是当他们尝试阅读帖子的全部内容时,他们会收到一些文字说“请注册以查看此内容”。

我已经在网上搜索过,试图找出是否有人以前实现过这一目标。我找到了下面的代码。它说将其作为 .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;

   }

}

【问题讨论】:

    标签: blogengine.net


    【解决方案1】:

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

    您不应授予会员查看未发布帖子的访问权限,因为这更多是为了让网站上的编辑能够在发布新帖子以供公众使用之前保存它们的草稿。

    据我了解 (?),只有您的朋友会在博客上写帖子,因此他应该是唯一拥有该权限的人。

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

    【讨论】:

    • 是的,没错。我只是在试验如何让它满足我朋友的要求。查看帖子时,我很难找到进行权限检查的位置。感谢您的反馈。
    • 默认情况下很可能没有检查,因为查看帖子的权限是使用管理页面设置的,但您应该能够在该视图中添加额外的逻辑来实现这一点。希望你让它工作! :)
    【解决方案2】:

    此问题现已解决。来自 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");
            }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多