Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default Roles not working

    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 Guest

  2. Similar Questions and Discussions

    1. Roles and Permissions??
      Jose, hopefully you are still around at this address and can help us out. We are doing what sounds to be exactly what you were trying to do; and...
    2. Get User Roles?
      In C#, I want to log in a user who has a specific role. The role was originally "Cashier". However, now the "Cashier" can be prefixed with...
    3. determining roles
      Hi all, I'm creating a web application that attempts to restrict access by checking the IsInRole function for the desired roles. This works for...
    4. Getting a list of roles
      This may have been answered in a previous post, and if so, please excuse my redundancy. I am using Windows authentication and I know about the...
    5. SQL App roles and intranet
      Hi, I have been charged with redesigning my companys intranet. The Intranet uses sql server as the backend db. Currently the intanet uses...
  3. #2

    Default Re: Roles not working

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

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

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


    "Neils Christoffersen" <NeilsChristoffersen@discussions.microsoft.com>
    escribió en el mensaje
    news:8C2A7EC2-7F42-4749-99EB-2431EC85D4DF@microsoft.com...
    >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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139