Search code examples
c#asp.net-mvcasp.net-identityidentity

ASP.NET Identity 2.0 IsInCompanyRole(role,company)


I want to create a method to find if the user has the 'Role' for specific 'Company'.

My 'UserRole' table contains 'CompanyId' as foreign key to the company table along with 'UserId' and 'RoleId'.

I am struggling to implement a method to use with the notation like 'User.IsInCompanyRole(role,company)', as the 'User.IsInRole' method.

Please guide me on this.


Solution

  • I found a solution as @Zoran mentioned. No need of claims for me right now. Create the extension method works like a charm. Extension Methods (C# Programming Guide)

    Here is my extention.

    public static class Extention
    {
        public static bool IsInCompanyRole(this IPrincipal user, string role, int companyId)
        {
            // Code goes here
        }
    }
    

    ans usage is simple like

    User.IsInEntityRole("Admin",1)