Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to add roles to user using Forms Authentication in ASP.NET 2.0

Reply
Thread Tools

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

 
 
Jules
Guest
Posts: n/a
 
      03-24-2006
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?

 
Reply With Quote
 
 
 
 
Erik Funkenbusch
Guest
Posts: n/a
 
      03-24-2006
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.
 
Reply With Quote
 
 
 
 
Jules
Guest
Posts: n/a
 
      03-24-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best practices for using forms authentication and security in a hosted env (was: Re: Using a Forms authentication in a shared hosting environment) JEFF ASP .Net 1 11-12-2007 07:00 PM
forms authentication -- expired forms cookie vs. not provided forms cookie Eric ASP .Net Security 2 01-27-2006 10:09 PM
NT based roles using forms authentication Sharat Koya ASP .Net 3 08-13-2004 06:58 PM
Forms Authentication question: How to have some pages open and some requiring forms authentication Eric ASP .Net 2 02-13-2004 02:14 PM
Forms Authentication +Active Directory +Roles Marty Underwood ASP .Net 4 10-30-2003 01:54 AM



Advertisments
 



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