Roles and SQL Server Access

Last post 12-12-2006 7:22 AM by DavidKiff. 2 replies.

Sort Posts:

  • Roles and SQL Server Access

    12-07-2006, 6:14 PM
    • Loading...
    • DavidKiff
    • Joined on 12-07-2006, 6:07 PM
    • Hertfordshire, UK
    • Posts 1,633

    Hi,

    I am trying to dynamically assign roles after authenticating against the active directory.  I would like to write

    Roles

    .AddUserToRole(txtUsername.Text, "staff");

    and the use the web.config file to only allow "staff" members to access a folder:

    <

    location path="staff">

    <
    allow roles="staff"/>

    ...etc

    I do not want to use a database at all for this, can i turn this off?  The other way is to create a session variable when a user logs in and check this every time a user accesses the staff folder?- although i would like to make use of the role feature in 2.0!

    Thanks in advance,

    David

     
    David Kiff
    http://DavidKiff.co.uk

    -- "Mark As Answer" If my reply helped you --
  • Re: Roles and SQL Server Access

    12-07-2006, 11:21 PM
    Answer
    • Loading...
    • DMW
    • Joined on 09-04-2002, 6:25 AM
    • Posts 1,924
    • Moderator

    You can't really do that. Roles.AddUserToRole() is really a way to add a role in the provider store, not just temporarily for the request.

    What you should be able to do, however, is the following.

    Handle the Application_AuthenticateRequest in global.asax, and then create a new GenericPrincipal object based on the current user to which you add the staff role, using code something like this:

    void

    Application_AuthenticateRequest( object sender, EventArgs e )
    {
     
    if (User.Identity.IsAuthenticated)
      {
        System.Collections.Generic.
    List<string> roles = new System.Collections.Generic.List<string>();
        roles.AddRange(
    Roles.GetRolesForUser());
        roles.Add(
    "staff");
        System.Security.Principal.
    GenericPrincipal p = new System.Security.Principal.GenericPrincipal(User.Identity, roles.ToArray());
        Context.User = p;
      }
    }

    I haven't had time to test this, but it should work fine.

    Dave
  • Re: Roles and SQL Server Access

    12-12-2006, 7:22 AM
    • Loading...
    • DavidKiff
    • Joined on 12-07-2006, 6:07 PM
    • Hertfordshire, UK
    • Posts 1,633

    Thats brilliant, worked fine i modified it a little to get the groups from AD.

     Thank you!

    David

    David Kiff
    http://DavidKiff.co.uk

    -- "Mark As Answer" If my reply helped you --
Page 1 of 1 (3 items)
Microsoft Communities
Page view counter