Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In a C# 2008 desktop application, I need change the application since all the users are moving from one domain to new domain. The current application does not let the user access information under the new domain name.
To accomplish this goal, I would like to see what roles have been setup for those users starting with myself.
To accomplish this goal, I would like to know how to access those roles.

I do know that when I look at: Thread.CurrentPrincipal.Identity.Name, I can access the person's name. I also know that if I use System.Security.Principal.WindowsIdentity.GetCurrent(), I get the same information.

However can you tell me in code how to access the subcategories in the following:
1. Thread.CurrentPrincipal.Identity
2. System.Security.Principal.WindowsIdentity

Can you tell me in tell or show me in code how to accomplish this goal?
Posted
Comments
Sushil Mate 30-Oct-12 23:41pm    
subcategory? what does it mean?

1 solution

Hello

I am not sure what the mean of "subcategories" but you can use following sample codes for role-based security;

code block for is in role checking;

C#
public bool IsInRole(string role)
{
    bool hasRole = System.Threading.Thread.CurrentPrincipal.IsInRole(role);
    return hasRole;
}


if your application running with a windows domain account role equal to user group's name.

A sample for securing a method;

C#
public void DoSecureMethod()
{
    // read roles
    bool hasRole1 = IsInRole("Role1");
    bool hasRole2 = IsInRole("Role2");

    if (hasRole1 & hasRole2)
    {
        // do some secure thing here
    }
    else
    {
        throw new ApplicationException("Your account has not enough right for to doing something...");
    }
}


other alternate is using declerative security;

C#
[PrincipalPermission(SecurityAction.Demand, Role = "Role1")]
public void DoAnotherSecureMethod()
{
    // this block secured by PrincipalPermissionAttribute, current user must have the role 'Role1'
}


Regards,
Tarik K.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900