Search code examples
c#asp.netmaster-pages

How to use common master page for different roles?


I want to use common master-page for different roles, i just want to set different themes and menu items according to roles, could somebody guide me on how can i use in my membership code in the same master page ?

For my current code i downloaded the membership code from the codeplex that set different master pages for different roles, but i see when i have to make some page common for all roles that is to be access according to roles, i have to make the page page in each roles folder and have to set it to master page menu according to role, so i want to use a common master page for all....


Solution

  • You can change the master page programmatically during the OnPreInit method:

    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);    
    
        if (Roles.IsUserInRole("Admins"))
        {         
            Page.MasterPageFile = "AdminDefault.master";
            return;
        }
    
        Page.MasterPageFile = "Default.master";
    }
    

    If this functionality will be used by several or more pages, I would consider putting it in a base class that the pages can inherit from.