我已經試圖嘗試獲得解決方案。BlogEngine.NET 3.3 - 防止匿名用戶做某些事情
我使用的是BlogEngine.NET 3.3。我有要求在博客中顯示300個角色的帖子,然後註冊用戶將點擊帖子名稱來閱讀其餘部分。
我想讓未註冊的用戶(匿名用戶)能夠看到300個字符,但是當他們嘗試閱讀帖子的全部內容時,他們會看到一些文字說「請註冊以查看此內容」。
我已經搜尋了網絡,試圖找出以前是否有人實現過。我找到了下面的代碼。它說它把它放到App_Code/Extensions文件夾中作爲一個.cs來啓用它。但是,在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;
}
}
是的沒錯。我只是試着讓它符合我的朋友的要求。在查看帖子時,我正在努力查找權限檢查的位置。感謝您的反饋意見。 – Matt
默認情況下,最有可能沒有檢查,因爲查看帖子的權限是使用管理頁面設置的,但您應該可以在該視圖中添加其他邏輯來完成此操作。 希望你得到它的工作! :) – MunchyYDL