Возможно, вы используете GenericPrincipal в Application_AuthenticateRequest. Я хотел бы предложить вам создать собственный принципал, который предоставляет массив ролей:
public class CustomPrincipal: IPrincipal
{
public CustomPrincipal(IIdentity identity, string[] roles)
{
this.Identity = identity;
this.Roles = roles;
}
public IIdentity Identity
{
get;
private set;
}
public string[] Roles
{
get;
private set;
}
public bool IsInRole(string role)
{
return (Array.BinarySearch(this.Roles, role) >= 0 ? true : false);
}
}
Теперь вы можете прочитать ваш файл cookie и создать собственный принципал.
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[My.Application.FORMS_COOKIE_NAME];
if ((authCookie != null) && (authCookie.Value != null))
{
var identity = new GenericIdentity(authTicket.Name, "FormAuthentication");
var principal = new CustomPrincipal(identity, Roles, Code);
Context.User = principal;
}
}
и ваша функция будет выглядеть примерно так:
public static IList<string> GetUserRoles(this HttpContextBase context)
{
if (context != null)
{
return(((CustomPrincipal)context.User).Roles);
}
return (null);
// return roles array;
}