.NETGURU
Is there a way to Enumerate the GenericPricipal object's roles array?
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngsec' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

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?

Reply to this message...
 
    
Yannick Smits (VIP)
You'll have to write your own method in your own IPrincipal =
implementation. I once did this to get a users first role (code below). =
You could write similar code to have it return a array of roles.

hth,
Yannick Smits

--start C# class code--
using System;
using System.Collections;
using System.Security.Principal;

namespace YannickSmits
{
    public class MyPrincipal : IPrincipal
    {
        private IIdentity m_identity;
        private ArrayList m_roles;

        public MyPrincipal(IIdentity identity, ArrayList roles)
        {
            m_identity =3D identity;
            m_roles =3D roles;
        }
    =09
        public MyPrincipal(IIdentity identity, string role)
        {
            m_identity =3D identity;
            m_roles =3D new ArrayList(1);
            m_roles.Add(role);
        }

        public bool IsInRole(String role)
        {
            if (m_roles =3D=3D null)
                return false;
            if (m_roles.Contains(role))
                return true;
            else
                return false;
        }

        public string FirstRole()
        {
            return m_roles[0].ToString();
        }

        public IIdentity Identity
        {
            get { return m_identity; }
        }
    }
}
--end C# class code--

----- Original Message -----
From: "Bill Bassler" <Click here to reveal e-mail address>
To: Click here to reveal e-mail address
Sent: Mon, 17 Jun 2002 08:44:28 -0400
Subject: Is there a way to Enumerate the GenericPricipal object's roles =
array?
[Original message clipped]

Reply to this message...
 
    
Bill Bassler
When I create an instance of the MyPrincipal class, the

FirstRole method is not visible, only the methods declared in the IPrincipal

interface such as IsInRole.

e.g

Original GenericPrincipal instantiation

app.Context.User = new GenericPrincipal (identity, roles);

app.Context.User.IsInRole();

New class instantiation

app.Context.User = new MyPrincipal (identity, roles);

app.Context.User.FirstRole(); <<<<< Method not found

"Yannick Smits" <Click here to reveal e-mail address> wrote in message
news:670958@aspngsec...

You'll have to write your own method in your own IPrincipal implementation.
I once did this to get a users first role (code below). You could write
similar code to have it return a array of roles.

hth,
Yannick Smits

--start C# class code--
using System;
using System.Collections;
using System.Security.Principal;

namespace YannickSmits
{
public class MyPrincipal : IPrincipal
{
private IIdentity m_identity;
private ArrayList m_roles;

public MyPrincipal(IIdentity identity, ArrayList roles)
{
m_identity = identity;
m_roles = roles;
}

public MyPrincipal(IIdentity identity, string role)
{
m_identity = identity;
m_roles = new ArrayList(1);
m_roles.Add(role);
}

public bool IsInRole(String role)
{
if (m_roles == null)
return false;
if (m_roles.Contains(role))
return true;
else
return false;
}

public string FirstRole()
{
return m_roles[0].ToString();
}

public IIdentity Identity
{
get { return m_identity; }
}
}
}
--end C# class code--

----- Original Message -----
From: "Bill Bassler" <Click here to reveal e-mail address>
To: Click here to reveal e-mail address
Sent: Mon, 17 Jun 2002 08:44:28 -0400
Subject: Is there a way to Enumerate the GenericPricipal object's roles
array?
[Original message clipped]

Reply to this message...
 
    
Yannick Smits
You'll have to cast it to your custom Principal object like:
((MyPrincipal)app.Context.User).FirstRole();

hth,
Yannick Smits

"Bill Bassler" <Click here to reveal e-mail address> wrote in message news:671208@aspngsec...
[Original message clipped]

Reply to this message...
 
    
Bill Bassler
Thanks. I was close but I was not casting properly.
"Yannick Smits" <Click here to reveal e-mail address> wrote in message
news:671217@aspngsec...
[Original message clipped]

Reply to this message...
 
 
System.Collections.ArrayList
System.Runtime.Remoting.Contexts.Context
System.Security.Principal.GenericPrincipal
System.Security.Principal.IIdentity
System.Security.Principal.IPrincipal




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