How to Restrict Method to particular User and class in c#


How to Restrict Method to particular User in c#





using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Inheritancet
{
    public class Person
    {
        [System.Security.Permissions.PrincipalPermission(System.Security.Permissions.SecurityAction.Demand, Role = "DoDelete")] // or "PowerUser" etc.
        public void DoDelete()
        {
            // Delete User Infor
            Console.WriteLine("gggg");         
        }
        public void click(string username)
        {

            // authenticate user here
            if (username != null)
            {
                GenericIdentity id = new GenericIdentity(username); // username of the current login

                Thread.CurrentPrincipal = new GenericPrincipal(id, new string[] { "Admin", "PowerUser", "DoDelete" }); // the roles the user has usually from a database
                DoDelete();
            }

        }
    }
     
    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            p.click("Admin");
            Console.Read();
        }
    }
}

Share this

Latest
Previous
Next Post »