Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

asp.net mvc custom attribute

Options
  • 04-04-2012 2:35pm
    #1
    Registered Users Posts: 872 ✭✭✭


    Hi,

    I am making an application that uses forms authentication. There is also a service in place that returns JSON info relating to the users and what they can see. Membership roles are not used in this case.

    2 items are passed back from the service, the application name and an array of rights that the user has. I need to be able to restrict access to certain views based on this info from the service.

    I created a custom attribute like so :
        public class CustomAuthorisationAttribute : AuthorizeAttribute
        {
            public string ApplicationName
    	public string[] Rights
    
    	//more logic to add here...
        }
    

    I then add this attribute to the controller so only certain users can view it.
        [CustomAuthorisation(ApplicationName="main",Roles= new [] {'isAdmin','canView')]
        public ActionResult Index()
        {
            return View();
        }
    


    I am wondering how i can compare what values are passed back from the service to what values are specified in the Attribute above the view ? The compare logic can determine whether to display the view or redirect them to another page on the site.

    I hope i have been clear

    Thanks


Comments

  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    You override the AuthorizeCore method, and there are others you can override as well.
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
               
        //logic to test if roles are valid.
        return SomeMethodWhichChecksRoles();
    }
    


Advertisement