Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to check authorization on a method for the current user to check if he/she has the right to perform this action.

Here is the example of the attribute I have in place:

C#
[AttributeUsageAttribute(AttributeTargets.Method)]
    public class IsAuthorized : Attribute
    {
        public IsAuthorized(Rights right)
        {
            bool isAuthorized = false;

            if (right == Rights.None)
                isAuthorized = true;
            else
            {
                DataAccessLayer.IDAL dal = new DataAccessLayer.DAL();
                string userName = Thread.CurrentPrincipal.Identity.Name;
                Guid userID = dal.GetUserIDFromUserName(userName);

                isAuthorized = dal.HasRight(userID, right.ToString());
            }

            if (!isAuthorized)
                throw new SecurityException("You don't have the rights to perform this action");
        }
    }

And this is how I how like to check if the user has the authority to access the method:

C#
[IsAuthorized(Rights.CreateUserGroup)]
    public string Ping()
    {
       return "The service is online";
    }
Posted

1 solution

Something like this might point you in the right direction;

using System;
using System.Security;
using System.Security.Permissions;

namespace AccessThingie {

    [Serializable]
    [AttributeUsageAttribute(AttributeTargets.Method)]
    public class IsAuthorized  : CodeAccessSecurityAttribute {
        private static readonly PrincipalPermission Allowed = new PrincipalPermission(PermissionState.None);
        private static readonly PrincipalPermission NotAllowed = new PrincipalPermission(PermissionState.Unrestricted);

        public static class Rights
        {
            public const string None = "None";
            public const string CreateUserGroup = "CreateUserGroup";
        }

        public string Right { get; set; }

        public IsAuthorized(SecurityAction action)
            : base(action) 
        {
        }

        public override IPermission CreatePermission()
        {
            return IsAuthorised(Right) ? Allowed : NotAllowed;
        }

        private static bool IsAuthorised(string right)
        {
            if (right == Rights.None)
                return true;
            else {
                /* Enable this
                DataAccessLayer.IDAL dal = new DataAccessLayer.DAL();
                string userName = Thread.CurrentPrincipal.Identity.Name;
                Guid userID = dal.GetUserIDFromUserName(userName);
                return dal.HasRight(userID, right);
                */
                return false;
            }
        }
    }

    class Program {

        [IsAuthorized(SecurityAction.Demand, Right = IsAuthorized.Rights.CreateUserGroup)]
        public static string Ping()
        {
            return "The service is online";
        }

        private static void Main(string[] args)
        {
            Ping();
        }
    }
}


Hope this helps,
Fredrik
 
Share this answer
 
Comments
Stiaan Jacobs 9-Jul-13 6:04am    
Thank you a million times. I have been sitting with this for a while now. I am new with these Security Access Controls.
Fredrik Bornander 9-Jul-13 7:21am    
Glad I could help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900