2012-08-01 90 views
1

在管理頁面中我有以下邏輯。我還需要Logs屏幕中的類似邏輯。因此,我打算將這個邏輯移動到基本頁面。在基本頁面中,如何識別當前頁面? (我如何區分管理員屏幕和日誌屏幕?)。在ASP.Net基礎頁面中獲取特定頁面信息頁面

根據頁面,配置中檢索的值不同。

實現此目的有哪些不同的方法?這些方法的最佳途徑是什麼?

 //Admin Screen 
     List<string> authorizedRoles = new List<string>((ConfigurationManager.AppSettings["AdminScreenRoles"]).Split(',')) 
     if (!authorizedRoles.Contains(userRole)) 
     { 
      Response.Redirect("UnauthorizedPage.aspx"); 
     } 

    //Logs Screen 
     List<string> authorizedRoles = new List<string>((ConfigurationManager.AppSettings["LogsScreenRoles"]).Split(',')) 
     if (!authorizedRoles.Contains(userRole)) 
     { 
      Response.Redirect("UnauthorizedPage.aspx"); 
     } 

回答

2

不要將代碼放在承認繼承它的類的基礎上。添加一個孩子將必須覆蓋的抽象屬性。 在基地:

​​

在日誌:

public override string AppSettingsRolesName 
{ 
    get { return "LogsScreenRoles"; } 
} 

在管理員:

public override string AppSettingsRolesName 
{ 
    get { return "AdminScreenRoles"; } 
} 
0

最簡單的方法是尋找到表單認證,因爲它會處理這一切的你通過一個配置文件。有許多在網路上散佈這個好文章 - 這裏有一個:

http://ondotnet.com/pub/a/dotnet/2003/01/06/formsauthp1.html

不過,如果你正在尋找一種快速解決,最簡單的方法是將您的代碼移動到基頁如你所說,並使用接口屬性來使繼承的頁面指出要使用的角色類型 - 例如沿着線的東西:

public abstract class BasePage : Page 
{ 
    protected abstract string AuthorisedRoles { get; } 

    protected override void OnLoad(EventArgs e) 
    { 
     base.OnLoad(e); 

     List<string> authorizedRoles = new List<string>((ConfigurationManager.AppSettings[this.AuthorisedRoles]).Split(',')); 
     if (!authorizedRoles.Contains(userRole)) 
     { 
      Response.Redirect("UnauthorizedPage.aspx"); 
     } 
    } 
} 

public class LogsPage : BasePage 
{ 
    protected override string AuthorisedRoles 
    { 
     get { return "LogsScreenRoles"; } 
    } 
} 

public class AdminPagePage : BasePage 
{ 
    protected override string AuthorisedRoles 
    { 
     get { return "AdminScreenRoles"; } 
    } 
} 

但嚴重的是,考慮窗體身份驗證,如果你想正確地做到這一點 - 它並不複雜,因爲它首先查找。