You must Sign In to post a response.
  • Category: ASP.NET

    How do send this authorize[role=''] by passing parameter

    Hi,
    I want send parameter to authorize[Rolle= Parameter]. Our organization want send parameter with role wise it can be admin, user, sales, general manager. sales manager and so on. I want sent by parameter to only action result methods.. instead Role.Administrator, Role.UserWithPrivileges) by sending parameter.

    controller
    =======
    [AuthorizeEnum(Role.Administrator, Role.UserWithPrivileges)]
    public IActionResult ThePrivilegeZone()
    {
    return View();
    }

    model
    ======
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
    public class AuthorizeEnumAttribute : AuthorizeAttribute
    {
    public AuthorizeEnumAttribute(params object[] roles)
    {
    if (roles.Any(r => r.GetType().BaseType != typeof(Enum)))
    throw new ArgumentException("roles");

    this.Roles = string.Join(",", roles.Select(r => Enum.GetName(r.GetType(), r)));
    }
    }

    public enum Role
    {
    Administrator = 1,
    UserWithPrivileges = 2,
    User = 3,
    }
  • #769643
    Hi Chandrasekhar,

    Find the below link. It may help you.
    http://www.ryanmwright.com/2010/04/25/dynamic-controlleraction-authorization-in-asp-net-mvc/
    https://www.codeproject.com/Tips/620420/MVC-Dynamic-Authorization

    I hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"


  • Sign In to post your comments