Roles not working

Discussion in 'ASP .Net Security' started by Neils Christoffersen, Nov 23, 2004.

  1. I have a quick question about ASP.NET roles.

    I have the following in in Global.asax:
    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
    Response.Write("Request authenticated: " + (Request.IsAuthenticated ? "yes"
    : "no") + "<br>");

    if (Request.IsAuthenticated)
    {
    string[] roles = new string[] {"ChangeUser", "MoveUser"};

    Thread.CurrentPrincipal = new GenericPrincipal(Context.User.Identity,
    roles);
    }
    }

    As I understand it, this should assign the "ChangeUser" and "MoveUser" roles
    to the authenticated user, but it doesn't seem to work (or I am doing
    something wrong). In my web form I have this:

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (Context.User.IsInRole("ChangeUser"))
    {
    Response.Write("<br>User is in role \"ChangeUser\"");
    }
    if (Context.User.IsInRole("MoveUser"))
    {
    Response.Write("<br>User is in role \"MoveUser\"");
    }
    }

    This does not print out the "User is in role ..." as I would expect it to.
    Any suggestions?
    Neils Christoffersen, Nov 23, 2004
    #1
    1. Advertising

  2. Change this line:
    Thread.CurrentPrincipal = new GenericPrincipal(Context.User.Identity,
    roles);

    To this:
    Context.User = new GenericPrincipal(Context.User.Identity,
    roles);

    --
    Hernan de Lahitte
    http://weblogs.asp.net/hernandl


    "Neils Christoffersen" <>
    escribió en el mensaje
    news:...
    >I have a quick question about ASP.NET roles.
    >
    > I have the following in in Global.asax:
    > protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    > {
    > Response.Write("Request authenticated: " + (Request.IsAuthenticated ?
    > "yes"
    > : "no") + "<br>");
    >
    > if (Request.IsAuthenticated)
    > {
    > string[] roles = new string[] {"ChangeUser", "MoveUser"};
    >
    > Thread.CurrentPrincipal = new GenericPrincipal(Context.User.Identity,
    > roles);
    > }
    > }
    >
    > As I understand it, this should assign the "ChangeUser" and "MoveUser"
    > roles
    > to the authenticated user, but it doesn't seem to work (or I am doing
    > something wrong). In my web form I have this:
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > if (Context.User.IsInRole("ChangeUser"))
    > {
    > Response.Write("<br>User is in role \"ChangeUser\"");
    > }
    > if (Context.User.IsInRole("MoveUser"))
    > {
    > Response.Write("<br>User is in role \"MoveUser\"");
    > }
    > }
    >
    > This does not print out the "User is in role ..." as I would expect it to.
    > Any suggestions?
    Hernan de Lahitte, Nov 24, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    3,594
  2. Homer
    Replies:
    5
    Views:
    826
    Homer
    Jul 20, 2007
  3. Jéjé
    Replies:
    0
    Views:
    220
    Jéjé
    Sep 27, 2005
  4. SJ
    Replies:
    3
    Views:
    328
    MikeS
    Feb 22, 2006
  5. rob lynch
    Replies:
    2
    Views:
    112
    rob lynch
    Mar 10, 2006
Loading...

Share This Page