1

Using a Asp.Net old project, to access webforms I need to create a custom class Attribute that reads users rights like 'Rights.ViewDashboard' or 'Rights.CanEdit' an so. The class code is:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
    public class AuthorizationAttribute : Attribute
    {
        public AuthorizationAttribute(Rights permission)
        {
            if (Security.IsAuthorizedTo(permission))
                return;
            HttpContext.Current.Server.TransferRequest("~", false);            
        }
    }

In the aspx webform I have:

[Authorization(Rights.ViewDashboard)]
public partial class DashboardRisorse : System.Web.UI.Page
 {
    protected void Page_Load(object sender, EventArgs e)
     {
     }       
 }

When the user calls the webform if he haven't specific right the page is not loaded and site is redirected to the default page. But if he make a refresh of the page the code isn't execute, attribute is ignored and the page is loaded. When debugging I see that this attribute is executed only once. Where is my fault? I don't need Net Core solution because the project has old assemblies. Thanks.

Ingd

1 Answer 1

1

I am assuming here that you are trying to define custom attribute similar to ActionFilter Attributes in MVC. Unfortunately ASP.Net does not work in the same way.

You have two options

  1. Create an HttpModule and use one of the events available to build your logic

  2. Write the logic you want to execute in Page Load. Use Page.IsPostBack to identify if it is initial load of the page or if the page is being posted back. Write the logic you need within the if.. else if conditions

In case my assumption was incorrect then please provide more details on your query specifically what is it that you are trying to achieve using the Attribute.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Now I understand. Your assumption is correct and I follow 2nd option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.