.NETGURU
Enumerate the GenericPrincipal's roles ?array
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngsec' list.


Bill Bassler
I've written some code that for each request, looks up user roles in a
database, creates a GenericPrincipal object and populates the roles assigned
to that user (see Application_AuthenticateRequest below)

I can then do things like:
Label3.Text += Context.User.Identity.Name.ToString() + "<br>";
Label3.Text += " is a member of role Manager: " + User.IsInRole("Manager") +
"<br>";
However, I would like to be able to enumerate all of the roles that the
"User" is assigned to.
Looking at the "User" object, I don't see any obvious ways to do this.
Would I need to inspect another object?

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
// HttpApplication refers to a particular instance of the
// ASP.Net application class that is generated to service a request
HttpApplication app = (HttpApplication) sender;

if (app.Request.IsAuthenticated && app.User.Identity is FormsIdentity)
{
FormsIdentity identity = (FormsIdentity) app.User.Identity;

// Retrieve the role(s) that the user belongs to
string[] roles = this.GetUserRoles (identity.Name);

// Create a GenericPrincipal containing the role name(s) and assign
it to the current request
if (roles.Length > 0)
app.Context.User = new GenericPrincipal (identity, roles);
}
}

Reply to this message...
 
    
Michael Iantosca
I did something similar except once I created the Principle object I
stored it in the cache using the user's GUID (each user in the database
has a GUID) as the key. The reason I did this was so I did not have to
re-query the db, create a principal and load it up with the roles each
time a secure page was viewed.

- Michael

-----Original Message-----
From: Bill Bassler [mailto:Click here to reveal e-mail address]
Sent: Monday, June 03, 2002 3:05 PM
To: aspngsec
Subject: [aspngsec] Enumerate the GenericPrincipal's roles ?array

I've written some code that for each request, looks up user roles in a
database, creates a GenericPrincipal object and populates the roles
assigned to that user (see Application_AuthenticateRequest below)

I can then do things like:
Label3.Text += Context.User.Identity.Name.ToString() + "<br>";
Label3.Text += " is a member of role Manager: " +
User.IsInRole("Manager") + "<br>"; However, I would like to be able to
enumerate all of the roles that the "User" is assigned to. Looking at
the "User" object, I don't see any obvious ways to do this. Would I need
to inspect another object?

protected void Application_AuthenticateRequest(Object sender, EventArgs
e) {
// HttpApplication refers to a particular instance of the
// ASP.Net application class that is generated to service a request
HttpApplication app = (HttpApplication) sender;

if (app.Request.IsAuthenticated && app.User.Identity is
FormsIdentity)
{
FormsIdentity identity = (FormsIdentity) app.User.Identity;

// Retrieve the role(s) that the user belongs to
string[] roles = this.GetUserRoles (identity.Name);

// Create a GenericPrincipal containing the role name(s) and
assign it to the current request
if (roles.Length > 0)
app.Context.User = new GenericPrincipal (identity, roles); }
}

| [aspngsec] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngsec.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
 
System.EventArgs
System.Runtime.Remoting.Contexts.Context
System.Security.Principal.GenericPrincipal
System.Web.HttpApplication
System.Web.Security.FormsIdentity




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification