Getting current windows user and groups

M

Mark

hey,
i'm trying to get the current windows user and the groups they are in.
Intergrated windows auth on and annoymous access turned off on IIS.

However when trying to compile the following code VS.net doesn't like
the User.IsInRole("domain"); With the error "type or namspace for User
not found".
I can't work out which reference i'm missing!

Basically the idea is to create a auth component that is called for
any authentication/authorization purposes.

Thanks for any help,



using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Security.Principal;

//Upon creation of this object set all varables to the current user
CurrentUser = WindowsIdentity.GetCurrent().Name;
isWinAuth = WindowsIdentity.GetCurrent().IsAuthenticated;

//check if the user is in any of these groups
//not the best, but usable

if (User.IsInRole("blah")) UserGroups.Add("blah");
if (User.IsInRole("blah")) UserGroups.Add("blah");
if (User.IsInRole("blah")) UserGroups.Add("blah");
 
M

Mark

Nope, System.Security still causes the same errors.

Is there another way to get/check which windows groups the user is in?

thanks
 
T

Tommy

Not sure if this is related to your problem, but I do know that in
some situation, the WindowsIdentity object will return a blank name.
To resolve this, you can change the authorization section in
Web.Config as follow:

<authorization>
<deny users="?" /> <!-- Allow all users -->
<allow users="*" />
<!-- <allow users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
<deny users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
-->
</authorization>

See if this helps your situation.

Tommy,
 
M

Mark

Nah it just won't even compile, let alone return a blank user. I think
User.Identity.Name; WindowsIdentity.GetCurrent().Name; must have to be
called from the direct page and not from a component or something.
thanks tho,

Not sure if this is related to your problem, but I do know that in
some situation, the WindowsIdentity object will return a blank name.
To resolve this, you can change the authorization section in
Web.Config as follow:

<authorization>
<deny users="?" /> <!-- Allow all users -->
<allow users="*" />
<!-- <allow users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
<deny users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
-->
</authorization>

See if this helps your situation.

Tommy,

hey,
i'm trying to get the current windows user and the groups they are in.
Intergrated windows auth on and annoymous access turned off on IIS.

However when trying to compile the following code VS.net doesn't like
the User.IsInRole("domain"); With the error "type or namspace for User
not found".
I can't work out which reference i'm missing!

Basically the idea is to create a auth component that is called for
any authentication/authorization purposes.

Thanks for any help,



using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Security.Principal;

//Upon creation of this object set all varables to the current user
CurrentUser = WindowsIdentity.GetCurrent().Name;
isWinAuth = WindowsIdentity.GetCurrent().IsAuthenticated;

//check if the user is in any of these groups
//not the best, but usable

if (User.IsInRole("blah")) UserGroups.Add("blah");
if (User.IsInRole("blah")) UserGroups.Add("blah");
if (User.IsInRole("blah")) UserGroups.Add("blah");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top