Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

asp.net mvc custom attribute

  • 04-04-2012 01:35PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 12,026 ✭✭✭✭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