2012-08-01 50 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"; } 
    } 
} 

但严重的是,考虑窗体身份验证,如果你想正确地做到这一点 - 它并不复杂,因为它首先查找。