从 HttpContextBase 获取角色

发布于 2024-10-19 02:25:53 字数 274 浏览 6 评论 0原文

有没有办法从 HttpContextBase 获取角色数组?

我正在寻找这样的课程:

    public static IList<string> GetUserRoles(this HttpContextBase context)
    {
        if (context != null)
        {

        }

        // return roles array;
    }

谢谢您的帮助。

There is a way to get the array of role from the HttpContextBase ?

I'm looking to do a class like that:

    public static IList<string> GetUserRoles(this HttpContextBase context)
    {
        if (context != null)
        {

        }

        // return roles array;
    }

Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿哥们儿 2024-10-26 02:25:53

您可以使用:

System.Web.Security.Roles.GetAllRoles()

您想使用 HttpContextBase 的原因是什么?

* 编辑 *
哎呀,我现在看到您想要给定用户的角色列表。
我以为你想要所有可用角色的列表。

您可以循环遍历角色并检查哪些角色适用:

HttpContextBase.User.IsInRole(role);

You can use:

System.Web.Security.Roles.GetAllRoles()

For what reason do you want to use HttpContextBase?

* EDIT *
Oops, I see now you want the list of roles for a given user.
I thought you wanted the list of all available roles.

you could loop through the roles and check which ones apply:

HttpContextBase.User.IsInRole(role);
风尘浪孓 2024-10-26 02:25:53

您可能在 Application_AuthenticateRequest 中使用 GenericPrincipal。我建议您创建一个公开一系列角色的自定义主体:

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;
    }

Probably you're using a GenericPrincipal in the Application_AuthenticateRequest. I would suggest you to create a custom principal which exposes an array of roles:

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);  
    }
} 

now you can read your cookie and create a custom principal.

    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;
        }
    }

and your function would look something like this:

    public static IList<string> GetUserRoles(this HttpContextBase context)
    {
        if (context != null)
        {
            return(((CustomPrincipal)context.User).Roles);
        }

        return (null);
        // return roles array;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文