Jeff Garoutte

c# .net and anything else that happens across my desk

Recent posts

Tags

Categories

Navigation

Pages

    Archive

    Blogroll

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    Extending the ASP.Net Security model to use rights : Part Three - Attributes

    Part Three- Attributes.  Now that we have a Principal object with rights loaded as the current requests user we can begin assigning security to code by the users rights as well as their role membership.  The objective here was to be able to tag code to require a right and assign that right to a role.  Any user within the role would have the right and be able to execute the code.  We have everything in place to do this, except the Attributes.

    Before e dive into Attributes let me recap where we have been; In part one of this series we covered the IPrincipal and right objects needed to secure code by rights under the asp.net membership and roles framework.  In part two we covered the IHttpModule that was required to make the custom RightPrincipal exist in the current request and we talked about a "RightManager".

    What is an Attribute?  The short answer is they add meta data to the code that can affect it's behavior.  Here is the code for the RightAttribute. 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Security.Permissions;
    
    namespace ObjectHelpDesk.Security
    {
        public class RightAttribute : CodeAccessSecurityAttribute
        {
            private String _userName;
            private String _rightName;
            private Boolean _authenticated;
    
            public RightAttribute(SecurityAction action)
                : base(action)
            {
                _authenticated = true;
                _userName = String.Empty;
                _rightName = String.Empty;
            }
            public Boolean Authenticated
            {
                get { return _authenticated; }
                set { _authenticated = value; }
            }
    
            public String RightName
            {
                get { return _rightName; }
                set { _rightName = value; }
            }
    
            public String UserName
            {
                get { return _userName; }
                set { _userName = value; }
            }
    
            public override System.Security.IPermission CreatePermission()
            {
                return new RightPermission(this._authenticated, this._userName, this._rightName);
            }
        }
    }

    There is not much to the Attribute code.  There is a UserName, RightName and Authenticated properties.  However, the CreatePermission() method returns a RightPermission.  This is what will check the current user to see if they have the needed right.

    You can add a RightPermission to any method that you would like to secure.  For example you could add it to a button event handler in the code behind of a web page.  I added two buttons to a default web page in the project and secured them as follows

    public partial class _Default : System.Web.UI.Page
    {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            [RightAttribute(System.Security.Permissions.SecurityAction.Demand,RightName="SVN Access")]
            protected void Button1_Click(object sender, EventArgs e)
            {
    ... } [RightAttribute(System.Security.Permissions.SecurityAction.Demand,RightName="Manager Access")] protected void Button2_Click(object sender, EventArgs e) {
    ... } }

    This will give us the ability to test everything after we have created the RightPermission.

    Before we dive into the RightPermission we need to revisit the RightManager.

    using System;
    using System.Collections.Generic;
    
    namespace ObjectHelpDesk.Security
    {
        public static class RightManager
        {
            public static List<Right> GetRightsByUserName(string userName)
            {
                List<Right> result = new List<Right>();
                Right someRight = new Right();
                someRight.Id = new Guid();
                someRight.RightName = "SVN Access";
    
                if (userName.ToLower().StartsWith("j"))
                    result.Add(someRight);
                return result;
            }
    
            public static List<Right> GetRightsByRoleName(string roleName)
            {
                List<Right> result = new List<Right>();
                Right someRight = new Right();
                someRight.Id = new Guid();
                someRight.RightName = "SVN Access";
    
                if (roleName.ToLower()=="developer")
                    result.Add(someRight);
                return result;
            }
        }
    }

    Notice we added a GetRightsByRoleName method.  Again it is a simple method and checks the RoleName to see if it equals "developer".   If it does the "SVN Access" right is added to the results.

    Finally we can work with the right Permission object....in Part Four - the RightPermission

    kick it on DotNetKicks.com

    Posted: May 27 2008, 01:17 by jeff | Comments (3) RSS comment feed |
    Filed under: Security

    Related posts

    Comments

    Talsja nl said:

    TalsjaDear Jeff,

    I have read your article and im very happy with your code. Now i have some requirements for an webapplication i have to develop for my company. There will be only 1 webapplication where every client who uses this application will have it's own database. This means that i update the membership, role and profile connectionstrings at runtime. Now the next requirement is dat every client can pick a role (these roles are predefined and the clients cannot change these)and for this role the client can set the access rights for the web application. This is my problem if you can see, because i can only set one web.config with access rules. If client a set's a rule that readers can access contactpage and client y set's a rule for the same page that readers cannot access this page i have a problem.

    To solve this problem i would like to implement your code. The only thing im struggling about is how to set the access rights only by role. In the current case when a button1 is clicked i get the access rights for the current user. But i always want it for the current user in role. You provide the code above(GetRightsByRoleName)but it's never accessed.

    Can you please help me understand how to implement your code so every time a user access a webpage i can check the access rights by rolename?

    Thank u very much

    Greatings Talsja

    # March 16 2009, 14:28

    Jeff us said:

    JeffTalsja,
    The default IPrinciple has a IsInRole functionm that checks the role name. If you are only intrested to see if the user is in a role use that. If you want to know if the user has a right, test for the right. Generally, I make GetRightsByUserName return back all rights for a user; including the rights the user gets from any roles they are a member of.

    Giving each user their own database can be a complex issue and changing the connection string(s) at runtime needs to be managed with great care to avoid "racing" and ensure thread safety.

    Good luck,
    Jeff

    # March 16 2009, 16:28

    jeffgaroutte.net said:

    pingbackPingback from jeffgaroutte.net

    Extending the ASP.Net Security model to use rights : Part two - the IHttpModule

    # June 19 2009, 18:47

    Add comment


    (Will show your Gravatar icon)  

      Country flag

    [b][/b] - [i][/i] - [u][/u]- [quote][/quote]