Question

I'm working on a Business Logic for a WPF-Application. Before the Application starts i have to prove if the CurrentUser is in the ActiveDirectory of a special Domain and if he is in there, i have to find out in which roles the current User is. Current user and his existence in AD is working fine but i have problems to find out the roles.

I've tried it with:

using System.Web.Security;

Roles.GetRolesForUser(currentuser);

but the Problem is, that i have to enable Role-Management so i wrote above the Code:

Roles.Enabled = true;

but there is still a problem with it --> System.InvalidOperationException;

Here's the whole Code (testversion):

string currentuser = Environment.UserName;
string currentmachine = Environment.MachineName;

if (DirectoryEntry.Exists(string.Format("WinNT://{0}/{1}", currentmachine,  currentuser)))
{
    Console.WriteLine("it's working\n");


    Roles.Enabled = true;
    Console.WriteLine(Roles.GetRolesForUser(currentuser));
}
else
{
    Console.WriteLine("it's not working");
}

Is it a problem with the using directive? Or are there other possibilities to check in which roles the user is?

thanks a lot.

Était-ce utile?

La solution

Maybe this could help you: Active Directory - Roles of a user

In this post the System.DirectoryServices.AccountManagement-Namespace is used. I think this is the better way for a WPF-Application and Roles.GetRolesForUser seems to be used in ASP-Web-Applications.

I hope this could help you.

Autres conseils

Try this if you're also using memberships

var user = Membership.GetUser("WhateverUsername");
string [] roles = Roles.GetRolesForUser(user.UserName);

If not then just specify the username:

string [] roles = Roles.GetRolesForUser("WhateverUsename");

This will get the roles for the username you provided. Hope it helps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top