How to add roles to user using Forms Authentication in ASP.NET 2.0

Discussion in 'ASP .Net' started by Jules, Mar 24, 2006.

  1. Jules

    Jules Guest

    When creating this website I user a custom authentication method to
    validate the usercredentials (I think the membership provider is an
    overkill since I only use the authentication part).

    When I have a authenticated user I use
    FormsAuthentication.RedirectFromLoginPage to authenticate the user in
    the ASP.NET context. However, I'd like to add roles to this
    GenericPricipal.

    Therefore I tried
    FormsAuthenticationTicket ticket = new
    FormsAuthenticationTicket(myUser.Name, false, 30);
    FormsIdentity userIdentity = new FormsIdentity(ticket);
    GenericPrincipal userPricipal = new GenericPrincipal(userIdentity,
    (string[])myUser.Rights.ToArray(System.Type.GetType("System.String")));
    HttpContext.Current.User = userPricipal;

    Two problems;
    1) When I navigate to a different aspx page within my application I
    lose this context and I'm not logged in anymore.
    2) When I call FormsAuthentication.RedirectFromLoginPage again, I lose
    the context also, but ASP.NET creates new one based on the username.
    But now I don't have the user-roles anymore.

    So:
    How can I add roles to my authenticated user in ASP.NET 2.0 when using
    FormsAuthentication?
    Jules, Mar 24, 2006
    #1
    1. Advertising

  2. On 23 Mar 2006 23:44:39 -0800, Jules wrote:

    > When creating this website I user a custom authentication method to
    > validate the usercredentials (I think the membership provider is an
    > overkill since I only use the authentication part).


    Well, ok.

    > When I have a authenticated user I use
    > FormsAuthentication.RedirectFromLoginPage to authenticate the user in
    > the ASP.NET context. However, I'd like to add roles to this
    > GenericPricipal.


    So, in other words, you're not just using authentication. you're also
    using roles.

    > So:
    > How can I add roles to my authenticated user in ASP.NET 2.0 when using
    > FormsAuthentication?


    Use the RoleProvider. I'm sure you'll also think that's overkill as well.
    Honestly, Membership and Rols are pretty well debugged and work very well.
    Why you feel the need to reinvent the wheel when using them is so brain
    dead simple is beyond me.
    Erik Funkenbusch, Mar 24, 2006
    #2
    1. Advertising

  3. Jules

    Jules Guest

    Erik,

    Thank you for your reply. And after some more investigation, I am
    taking back my statement about role/membership provider being a
    overkill. Since I'm very new to this Provider-thing, my vision about
    this was not clear.

    Anyway, the solution is actually very easy when using the
    RoleProvider-model. I just created my own RoleProvider, and only
    implemented the following method.

    public override string[] GetRolesForUser(string username)
    {
    string[] rolesForUser = <LOGIC THAT FETCHES USER ROLES>
    return rolesForUser;
    }

    After registering this to the web.config, it worked like a charm. No I
    can easily use the XmlSiteMap provider to customize my menu, by just
    setting "roles=".

    It even gets better. When implementing this solution[1], I can manage
    the role-rights in my application in one place: web.sitemap. When using
    this, I don't have to duplicate the roles in de web.config.

    Kind regards,

    Jules

    [1] http://www.codeproject.com/aspnet/aspnet2security.asp
    Jules, Mar 24, 2006
    #3
    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. Stefan Kiryazov

    Roles in ASP.NET forms authentication

    Stefan Kiryazov, Jan 31, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,420
    Teemu Keiski
    Jan 31, 2004
  2. Eric
    Replies:
    2
    Views:
    1,354
    Tommy
    Feb 13, 2004
  3. Sharat Koya

    NT based roles using forms authentication

    Sharat Koya, Aug 13, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,159
    Scott Allen
    Aug 13, 2004
  4. JEFF
    Replies:
    1
    Views:
    996
    =?Utf-8?B?YnJpYW5zW01DU0Rd?=
    Nov 12, 2007
  5. Eric
    Replies:
    2
    Views:
    450
Loading...

Share This Page