Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET网页上使用属性进行身份验证?_C#_Asp.net_Attributes_Webforms - Fatal编程技术网

C# 如何在ASP.NET网页上使用属性进行身份验证?

C# 如何在ASP.NET网页上使用属性进行身份验证?,c#,asp.net,attributes,webforms,C#,Asp.net,Attributes,Webforms,我有一个网站,它有几个从PageBase类派生的aspx页面。例如,下面是其中之一: public partial class Pages_Home_Default : PageBase { } 在其中一些页面中,我希望阻止访问,除非登录。我可以通过IsMember属性获取客户端是否登录到我的PageBase中 我想用态度来实现这一点。例如: [AuthenticationRequired(true)] public partial class Pages_Home_Default : Pag

我有一个网站,它有几个从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; }
}
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
}
例如,在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; }
}
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);
但是,当您希望在基类而不是派生类上执行此操作时,这是不实际的

这能做到吗


非常感谢您

如果您使用MVC,这里有一个att-


如果您使用的是WebForms,那么您不需要使用属性,您可以使用元素从web.config控制该属性。

如果您使用的是MVC,则有一个att-


如果您正在使用WebForms,那么您不需要使用属性,您可以使用元素从web.config控制它。

为什么不在属性本身中检查它

[AttributeUsage(AttributeTargets.Class)]
public class AuthenticationRequired : Attribute
{
    public AuthenticationRequired(bool isMemberRequired)
    {
        if(isMemberRequired && !HttpContext.Current.User.Identity.IsAuthenticated)
        {
          FormsAuthentication.RedirectToLoginPage();
        }
    }
}

为什么不在属性本身中检查它呢

[AttributeUsage(AttributeTargets.Class)]
public class AuthenticationRequired : Attribute
{
    public AuthenticationRequired(bool isMemberRequired)
    {
        if(isMemberRequired && !HttpContext.Current.User.Identity.IsAuthenticated)
        {
          FormsAuthentication.RedirectToLoginPage();
        }
    }
}

嗯。我将前面给出的代码与其他来源的一行简单代码结合起来,下面是我提出的代码:

[AttributeUsage(AttributeTargets.Class)]
public class AuthenticationRequired : Attribute
{
    public AuthenticationRequired(bool isMemberRequired)
    {
        Value = isMemberRequired;
    }

    public bool Value { get; private set; }
}


public class Utility
{
    public static T GetCustomAttribute<T>(Type classType) where T : Attribute
    {
        object Result = null;

        System.Reflection.MemberInfo Info = classType;

        foreach (var Attr in Info.GetCustomAttributes(true))
        {
            if (Attr.GetType() == typeof(T))
            {
                Result = Attr;
                break;
            }
        }
        return (T)Result;
    }
}

public class PageBase : System.Web.UI.Page
{
    protected override void OnPreInit(EventArgs e)
    {
        AuthenticationRequired AttrAuth = Utility.GetCustomAttribute<AuthenticationRequired>(this.GetType());

        if (AttrAuth != null && AttrAuth.Value)
        {
            if(!IsMember)
                HttpContext.Current.Response.Redirect("Membership.aspx");
        }
    }
}
[AttributeUsage(AttributeTargets.Class)]
需要公共类身份验证:属性
{
需要公共身份验证(bool isMemberRequired)
{
值=isMemberRequired;
}
公共布尔值{get;private set;}
}
公用事业
{
公共静态T GetCustomAttribute(类型classType),其中T:Attribute
{
对象结果=空;
System.Reflection.MemberInfo=classType;
foreach(Info.GetCustomAttributes中的var Attr(true))
{
if(Attr.GetType()==typeof(T))
{
结果=Attr;
打破
}
}
返回(T)结果;
}
}
公共类PageBase:System.Web.UI.Page
{
受保护的重写void OnPreInit(EventArgs e)
{
AuthenticationRequired AttrAuth=Utility.GetCustomAttribute(this.GetType());
if(AttrAuth!=null&&AttrAuth.Value)
{
如果(!IsMember)
HttpContext.Current.Response.Redirect(“Membership.aspx”);
}
}
}

好的。我将前面给出的代码与其他来源的一行简单代码结合起来,下面是我提出的代码:

[AttributeUsage(AttributeTargets.Class)]
public class AuthenticationRequired : Attribute
{
    public AuthenticationRequired(bool isMemberRequired)
    {
        Value = isMemberRequired;
    }

    public bool Value { get; private set; }
}


public class Utility
{
    public static T GetCustomAttribute<T>(Type classType) where T : Attribute
    {
        object Result = null;

        System.Reflection.MemberInfo Info = classType;

        foreach (var Attr in Info.GetCustomAttributes(true))
        {
            if (Attr.GetType() == typeof(T))
            {
                Result = Attr;
                break;
            }
        }
        return (T)Result;
    }
}

public class PageBase : System.Web.UI.Page
{
    protected override void OnPreInit(EventArgs e)
    {
        AuthenticationRequired AttrAuth = Utility.GetCustomAttribute<AuthenticationRequired>(this.GetType());

        if (AttrAuth != null && AttrAuth.Value)
        {
            if(!IsMember)
                HttpContext.Current.Response.Redirect("Membership.aspx");
        }
    }
}
[AttributeUsage(AttributeTargets.Class)]
需要公共类身份验证:属性
{
需要公共身份验证(bool isMemberRequired)
{
值=isMemberRequired;
}
公共布尔值{get;private set;}
}
公用事业
{
公共静态T GetCustomAttribute(类型classType),其中T:Attribute
{
对象结果=空;
System.Reflection.MemberInfo=classType;
foreach(Info.GetCustomAttributes中的var Attr(true))
{
if(Attr.GetType()==typeof(T))
{
结果=Attr;
打破
}
}
返回(T)结果;
}
}
公共类PageBase:System.Web.UI.Page
{
受保护的重写void OnPreInit(EventArgs e)
{
AuthenticationRequired AttrAuth=Utility.GetCustomAttribute(this.GetType());
if(AttrAuth!=null&&AttrAuth.Value)
{
如果(!IsMember)
HttpContext.Current.Response.Redirect(“Membership.aspx”);
}
}
}

+1,这是一条吸引人的口号。不过,你最好小心,可能有人已经申请了类似的专利。我猜他使用的是ASP.NET Webforms,这是MVC属性。是的。这是MVC,我在MVC上使用了类似的东西,但我的问题在于普通的Asp.NET网站,而不是MVC。由于给定的要求,有一个自定义的身份验证系统。因此,我不认为我可以使用windows的角色管理系统,该系统是在web.config.+1中配置的,用于吸引人的口号。不过,你最好小心,可能有人已经申请了类似的专利。我猜他使用的是ASP.NET Webforms,这是MVC属性。是的。这是MVC,我在MVC上使用了类似的东西,但我的问题在于普通的Asp.NET网站,而不是MVC。由于给定的要求,有一个自定义的身份验证系统。因此,我认为我不能使用在web.config中配置的windows角色管理系统。谢谢,您的方法非常可靠,但是这个类“class AuthenticationRequired”无法到达确定客户端是否经过身份验证的参数。我必须检查会话和其他一些东西,这将导致我在这个类中复制与weell相同的方法。我想避免这种情况。谢谢,您的方法非常合理,但是这个类“class AuthenticationRequired”无法到达确定客户端是否经过身份验证的参数。我必须检查会话和其他一些东西,这将导致我在这个类中复制与weell相同的方法。我想避免这种情况。