我有一个网站,其中有几个从 PageBase 类派生的 aspx 页面。例如其中之一如下:
public partial class Pages_Home_Default : PageBase
{
}
在其中一些页面中,我想阻止访问,除非登录。我可以使用 IsMember 属性在我的 PageBase 中获取客户端是否登录。
我想使用属性来实现这一点。例如:
[AuthenticationRequired(true)]
public partial class Pages_Home_Default : PageBaseList
{
}
[AttributeUsage(AttributeTargets.Class)]
public class AuthenticationRequired : Attribute
{
public AuthenticationRequired(bool isMemberRequired)
{
Value = isMemberRequired;
}
public bool Value { get; private set; }
}
例如在 PageBase 中:
protected override void OnPreInit(EventArgs e)
{
//Retrieve the AuthenticationRequired attribue value and if not authenticated Redirect client to a login page if logged in, continue displaying the page
}
我还发现这个来获取和读取属性
System.Reflection.MemberInfo info = typeof(Pages_Home_Default);
object[] attributes = info.GetCustomAttributes(true);
但是,当您想在 BASE 类而不是 DERIVED 类上执行此操作时,这是不切实际的。
这可以做到吗?
非常感谢你