473,473 Members | 4,208 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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?

Mar 24 '06 #1
2 2484
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.
Mar 24 '06 #2
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

Mar 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Phil Townsend | last post by:
I am integrating forms authentication in a web app which authenticates users who are listed in a SQL Server table. There will be a small list of application roles associated with each user and I...
4
by: Marty Underwood | last post by:
Okay the subject line explains a scenario I just had to tackle but I am looking for a better way. The current way: 1) Use forms authentication. 2) Query Active Directory and bind to a user...
3
by: Sharat Koya | last post by:
Please can you help with a problem I am having. My web config is set to... <authorization><deny users="?"/> <authentication mode="Forms"> <forms name=".COOKIE" loginUrl="login.aspx"...
2
by: Joey | last post by:
I have a web app with many users and their associated values (hashed passwords, first name, last name, etc...) stored in a Microsoft SQL Server 2000 database. My app uses stored procedures and...
1
by: Mark Olbert | last post by:
I'm building an ASPNET2 website which uses forms authentication but does not use the Microsoft-supplied membership providers (mostly because I don't want to create my own provider at this point, and...
5
by: Andrew | last post by:
Hi, I have a default.aspx which allows the user to choose between module Admin and module B. When the user clicks either one, he will be redirected to a FormsAuthentication login page. The...
1
by: PaulG | last post by:
Hi I'm trying to implement roles onto a .NET2 enviroment running IIS using Form Authentication. Using default aspnet login screen working back to a SQL server. The Web. config for directory is...
2
by: mark4asp | last post by:
This is a simplified version of my site. There are Premium users who have access to the Premium directory. Anyone else attempting to access it should be logged and then redirected to the...
2
by: trullock | last post by:
Hi, I need to control access to certain pages in my site, via the use of forms based authentication and roles. Now i've got the pages i want protected by a login form and thats all working...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.