Affichage des articles dont le libellé est asp.net MVC. Afficher tous les articles
Affichage des articles dont le libellé est asp.net MVC. Afficher tous les articles

mercredi 21 juillet 2010

User/role extension methods in an ASP.NET MVC view

This is a small Html helper that I use when I need to check the role in a view

public static class PageHelper
{
public static bool IsAdmin(this ViewUserControl pg)
{
return pg.Page.User.IsInRole("Admin");
}

public static bool IsCustomer(this ViewUserControl pg)
{
return pg.Page.User.IsInRole("Customer");
}
}
Don't forget to add the namespace for your extension methods in your Web.config :
<namespaces>
<add namespace="MyProject.Web.Helpers" />
</namespaces>