2016-05-12 29 views
0

我已經試圖嘗試獲得解決方案。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; 

    } 

} 

回答

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

好了,前一段時間我用BlogEngine.Net,我會盡力幫助你從我的腦海的頂部,所以我真的不知道我的回答是正確的,但也許它會給你一些指示,好嗎?

您不應該讓會員有權查看未發佈的帖子,因爲這對於網站上的編輯更爲重要,因爲他們可以在發佈新帖子以供公衆消費之前保存新帖子的草稿。

根據我的理解(?),只有你的朋友會在博客上寫帖子,因此他應該是唯一擁有該權限的人。

有一件事可能會起作用,那就是讓每個人都有權看帖子,如果需要這樣才能讓第一頁工作(我真的不記得)。然後,您可以覆蓋/自定義顯示帖子的控件/視圖,在那裏您可以檢查用戶是否實際註冊並決定顯示帖子或提示他們註冊的消息。

+0

是的沒錯。我只是試着讓它符合我的朋友的要求。在查看帖子時,我正在努力查找權限檢查的位置。感謝您的反饋意見。 – Matt

+0

默認情況下,最有可能沒有檢查,因爲查看帖子的權限是使用管理頁面設置的,但您應該可以在該視圖中添加其他邏輯來完成此操作。 希望你得到它的工作! :) – MunchyYDL

相關問題