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



我重新措辞是为了尝试获得解决方案。

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

我希望未注册的用户(匿名用户)能够看到这300个字符,但当他们试图阅读帖子的全部内容时,会收到一些文字,上面写着"请注册查看此内容"。

我在网上搜索过,想知道是否有人以前做到过这一点。我找到了下面的代码。它说将它作为一个.cs放入App_Code/Extensions文件夹中以启用它。然而,在3.3中,App_Code中没有扩展文件夹。这里有一个BlogEngine。核心\Web\扩展。我试着把下面的代码放进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,我会尽力帮助你,所以我真的不确定我的答案是否正确,但也许它会给你一些建议,好吗?

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

据我所知(?),只有你的朋友会在博客上发帖,因此他应该是唯一一个获得许可的人。

有一件事可能会奏效,那就是允许每个人观看帖子,如果这是第一页工作所必需的(我真的不记得了)。然后,你可以覆盖/自定义显示帖子的控件/视图,在那里你可以检查用户是否真的注册了,并决定显示帖子或告诉他们注册的消息。

此问题现已解决。rtur来自BlogEngine。Net对此提供了善意的帮助。

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

最新更新