Spontaneous Publicity
blogs are the new phone book

Finding Out which Groups a User is a Member Of When Using Windows Authentication in Asp.Net

March 13, 2008 08:44 by Luke

When using Windows authentication with Asp.net, I often need to know which active directory groups a user is a member of. Now I know that you can do something like:

[code=csharp] if (User.IsInRole("Admin")) { //Give Access to Secrets } [/code] The problem with this is you need to know the name of the group ahead of time. And what if you are on a network where the full name of a group is not always clear. The actual group name may be "MyDomain\Admin". So I wrote up a quick way to just get a list of all the groups a user is a member of. It isn't super straight forward (as far as which types you need to cast to) so I thought I would list it out here: [code=csharp] public static List GetGroups(RolePrincipal user) { List groups = new List(); WindowsIdentity identity = p.Identity as WindowsIdentity; foreach (IdentityReference group in identity.Groups) { NTAccount account = (NTAccount)group.Translate(typeof(NTAccount)); groups.Add(account.Value); } return groups; } [/code] the user of it on a web page would be something like: [code=csharp] List groups = GetGroups(User as RolePrincipal); [/code]

Keep in mind that this is assuming you are using Windows Authentication. So the weird part of the code above is:

[code=csharp] NTAccount account = (NTAccount)group.Translate(typeof(NTAccount)); [/code]

if you do not get this step, you will just get a bunch of Active Directory IDs that won't do you much good.

Also, sorry about the long title. I just can't think of a clever title today. Maybe I should add something like "Ultimate Edition for Developers" on the end to make it extra clear.

kick it on DotNetKicks.com
Tags:
Categories:
Actions: E-mail | del.icio.us | Permalink | Comments (84) | Comment RSSRSS comment feed

Comments

July 11. 2010 19:27

Islam Blogger

We tried to make it as we also build new Free <A href="http://islamblogger.com">Islam Blogger</A> Hosting islamblogger.com on internet for all newbie muslim blogger, support and spread us on internet

Islam Blogger

July 12. 2010 19:41

Dellwo13417@hotmail.com

Fantastic article, a bunch of really good facts.  I am going to show my friends and ask them the things they think.

Dellwo13417@hotmail.com

July 12. 2010 23:52

Stephana

It's so refreshing to find articles like the ones you post on your site. Very informative reading. I will keep you bookmarked. Thanks!

Stephana

July 15. 2010 22:44

china phone

This blog rocks! I gotta say, that I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I¡¯m glad I found your blog. Thanks,A definite great read¡­

china phone

July 17. 2010 14:09

How To Get Rid Of Cellulite

Hey ...compliments for the very informative article.I'm really happy I found it on bingKeep up the great work because I for sure will visit for updates

How To Get Rid Of Cellulite

July 18. 2010 01:45

Free Payroll Software

Thought-provoking, but not optimal.  When will we know more?  

Free Payroll Software

July 18. 2010 10:58

Nick Slater

Hi just thought i'd tell you something. This is the second time now i've landed in your blog in the last four days hunting for totally unrelated things. Spooky or what?

Nick Slater

July 21. 2010 00:04

Issac Maez

I think I will have to blog about this as well...

Issac Maez

July 21. 2010 00:47

Free Payroll Software

Great post.  Very good ideas.  Gracias!

Free Payroll Software

July 21. 2010 12:49

hotmail password

Following searching for a legitimate hotmail password hack program, I identified this application. Be careful with it though, I'm not sure if whatsoever legal to use it to hack someones hotmail or yahoo account password, its only legal to utilize it your self when you ever neglect the password. http://www.hotmailpasswordhack.net

hotmail password

July 22. 2010 02:03

Charlsie Vieths

Wholly crap batman! this will definately turn my night into somethng other than boring ole tv time. Thank you.

Charlsie Vieths

July 23. 2010 04:01

Tyson F. Gautreaux

Its good to be back in your blog. I found lot of something new again and I learn a lot again. Let me give credits in your blog... Thank you so musch and post more contents again.....

Tyson F. Gautreaux

July 23. 2010 17:40

Kim Kardashian tape

this story looks familiar...

Kim Kardashian tape

July 23. 2010 21:59

Improve Spelling

I got an error code 7119 on your blog.  Thought that I should tell you.

Improve Spelling

July 24. 2010 00:07

Gregory Despain

what plugins are you using on your blog?

Gregory Despain

July 25. 2010 04:32

Luigi Fulk

why are you not adding more revenue sources to your blog?

Luigi Fulk

July 25. 2010 08:40

i-link 8000

Nice post.I love reading your blog.

i-link 8000

July 26. 2010 02:52

audemar piguet

I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.Any way Ill be subscribing to your feed and I hope you post again soon

audemar piguet

July 26. 2010 02:53

cartie pasha watch

Sounds great!

cartie pasha watch

July 26. 2010 11:59

Gregory Despain

how old is this blog?

Gregory Despain

July 27. 2010 03:54

Andrew A. Sailer

I want to know your secret to have so many comments...

Andrew A. Sailer

July 28. 2010 06:25

Abu Dhabi Accommodation

I like your post & I will always be coming frequently to read more of your post. Thank you very much for your post once more.

Abu Dhabi Accommodation

July 28. 2010 06:51

LunarPages Review

Cheers for this blog post, It's great to see another BlogEngine user. Most users these days tend to use other systems like Wordpress, but I think BlogEngine is the best system to use.

LunarPages Review

July 28. 2010 10:56

building materials in dubai

Thanks for sharing info. Keep up the good work...We hope you will visit our blog often as we discuss topics of interest to you.

building materials in dubai

July 28. 2010 11:21

Dubai Property

I havent any word to appreciate this post.....Really i am impressed from this post....the person who generate this post it was a great human..thanks for shared this with us

Dubai Property

July 28. 2010 11:21

Property in Dubai


Wow. This Blog is truly a gold mine. I will actually try these tips and let you know how they work out! Thanks again mate.

Property in Dubai

July 28. 2010 21:07

natural cures for bacterial vaginosis

Very worthwhile page.  Your current site is swiftly growing to be one of my favorites.

natural cures for bacterial vaginosis

July 29. 2010 15:18

Tyson F. Gautreaux

I have read about this on newspaper a few week ago...

Tyson F. Gautreaux

August 1. 2010 00:09

Matthew C. Kriner

how old is this blog?

Matthew C. Kriner

August 1. 2010 21:09

christian dior wallets

Thanks for such a great post and the review, I am totally impressed! Keep stuff like this coming.

christian dior wallets

August 3. 2010 02:53

Tabatha Probert

I am writing a book about this. Will you mind answering some questions?

Tabatha Probert

August 4. 2010 00:51

iPhone news

Ha, actions speak louder than words. Come on guys!

iPhone news

August 5. 2010 04:45

tattoo book

you can write a book about this...

tattoo book

August 7. 2010 22:56

get a girl

As an amateur writer from Canada, I rarely come across really quality publications like this blog which really helps me inspirationally. It shows me what great articles are all about. There are tons of low quality writings on the web and it's very sad. Thank you for your well written posts! I frequently read your blog and enjoy the content you share. I look forward to more of your work.

get a girl

August 8. 2010 14:49

Tana Rizzuto

nice post, have a great week end!

Tana Rizzuto

August 8. 2010 16:13

Brain Exercises

Thanks for the intriguing information.  Please meditate upon the many good things in this world, and spread the word.  It is a great honor to pass along joy.  Work hard to provide something nice for someone else whenever you have the opportunity.  Make the world more special for all living things.

Brain Exercises

August 9. 2010 07:31

Shanta Baisden

what about adding some pics too...

Shanta Baisden

August 10. 2010 08:41

metal detecting books

anyone interested in metal detecting?

metal detecting books

August 10. 2010 14:08

Swimming Tips

Yikes!  Love your blog.

Swimming Tips

August 10. 2010 16:08

Meri Antill

Awesome article, loads of helpful information.

Meri Antill

August 12. 2010 18:53

e-cig

Come on guys, let's keep it clean. This is a good blog. Good info mate.

e-cig

August 12. 2010 19:37

Cursos-de-ingles-en-el-extranjero

It's crazy this gets written about so little, I'm sure the demand is there.

Cursos-de-ingles-en-el-extranjero

August 14. 2010 09:00

Sanford Bruson

a lot of comments on this post...

Sanford Bruson

August 15. 2010 04:21

acne diet

what about giving more details?

acne diet

August 15. 2010 18:31

ebt

Ha, can't say you're wrong. Just need to get my head around this.

ebt

August 16. 2010 19:56

Cursos de inglés

It's the little things that matter, that's what I believe.

Cursos de inglés

August 16. 2010 23:09

electronic-cigarette

It's crazy this gets written about so little, I'm sure the demand is there.

electronic-cigarette

August 17. 2010 02:03

dvd to itouch

Great post. I love reading.

dvd to itouch

August 17. 2010 06:40

Lekisha Michetti

any plan to post more frequently?

Lekisha Michetti

August 18. 2010 04:14

Terry Limbert

busy blog...

Terry Limbert

August 18. 2010 05:19

Porter Cable Battery

I think you have thought about adding some video clips to your weblog posts to keep your fans more entertained? I mean I just read through the entire piece here and it was good but since folks are more of a visual learner, I found that to be more helpful. Well you should let me know how it turns out! I love what you guys are doing and up too. Such interesting work and reporting! Keep up the great work guys. I've added this place to my page links. This is a cool blog thanks for sharing this informative information.. I will visit your blog regularly for some latest post.

Porter Cable Battery

August 18. 2010 07:59

Binary options

Fabulous article ,The things which are added here this is truely useful for most of the people. All the information is good explained ,This post help me to sortout my some problem.This is great,Thanks for sharing,

Binary options

August 19. 2010 04:01

Kassandra Tent

a contact form will be useful...

Kassandra Tent

August 19. 2010 22:30

Broyhill Furniture

Thanks for this post,  it was great to read.

Broyhill Furniture

August 19. 2010 22:30

Broyhill Furniture

Cheers for this post,  it was great to read.

Broyhill Furniture

August 20. 2010 01:56

acne scar

how can I advertise on this site?

acne scar

August 21. 2010 02:41

Isiah Hugee

are you selling ads on your blog?

Isiah Hugee

August 21. 2010 14:50

mean blonde jokes

anyone know some good jokes?

mean blonde jokes

August 21. 2010 15:22

Fat Loss for Idiots Review

I think you may be right in what you have said in this post. Whoever there are many people will disagree with you.

Fat Loss for Idiots Review

August 23. 2010 00:15

blonde jokes one liners

want a good blonde joke?

blonde jokes one liners

August 23. 2010 22:27

best personal training certification

anyone want to be a personal trainer?

best personal training certification

August 25. 2010 08:10

gift guides

any idea what is the best gift for women?

gift guides

August 26. 2010 00:19

get laid books

anyone know a good seduction book?

get laid books

August 27. 2010 12:28

botkier bags

All I can say is... please write more. OMG, it seems as though I watched a video instead of reading an article...it's so crystal clear. You actually know what you're talking about. My thought was,  why waste your knowledge on blog posting when you could be authoring books? Think about it. You're awesome at what you do. WORD!

botkier bags

August 27. 2010 20:35

Chad Skovira

do you have an rss feed? I want to add it to my reader but I can't find it...

Chad Skovira

August 28. 2010 08:51

Chasity Kuhns

why not posting more?

Chasity Kuhns

August 29. 2010 11:54

Angelo Torchia

I bookmarked your blog but I hope you will post more...

Angelo Torchia

August 29. 2010 23:53

Web Based Accounting Software

Saw an error message code 6514 when this page loaded.  I thought you should know.

Web Based Accounting Software

August 30. 2010 14:40

dating affiliate

you may make some money if you add an affiliate program to your blog...

dating affiliate

August 30. 2010 22:28

dating tips

great blog! I too have one ob dating and seduction tips for guys...

dating tips

August 31. 2010 01:36

blonde jokes

nice blog! keep the good work!

blonde jokes

August 31. 2010 20:32

refurbished garmin gps

I uncovered your blog Wednesday and started follow your posts regularly. I have not commented on any blog just yet but I was contemplating I would love to. It is pretty exciting to actually contribute to a publication even if it's only a blog. I really don't know exactly what to say other than I genuinely enjoyed reading through 2 of the articles. Nice articles indeed. I sure will keep visiting your blog daily. I learned quite a bit from you. Thx!

refurbished garmin gps

September 1. 2010 08:57

personal training

don't spend all the day in front of your computer, go out, exercise, go to the gym twice a week...

personal training

September 2. 2010 02:17

ETFs

it will be great if you post more...

ETFs

September 2. 2010 14:49

gift ideas

try to change your template, there are some great ones...

gift ideas

September 2. 2010 16:54

Phylicia Crowley

i thought this was a really great post to read.  i'll check back for new posts by you!

Phylicia Crowley

September 3. 2010 21:33

mixed wrestling

Thank you for making the sincere effort to divulge this. I feel very strong about it and would like to learn more. If it's possible, as you attain more in depth knowledge, would you mind adding more articles similar to this one with more information? It would be extremely helpful and useful for me and my friends.

mixed wrestling

September 4. 2010 10:29

dating

do you have a xml feed for your blog?

dating

September 5. 2010 07:44

Bentonite Clay

Come on guys, let's keep it clean. This is a good blog. Good info mate.

Bentonite Clay

September 6. 2010 03:07

ETF

just bookmarked your blog...

ETF

September 6. 2010 23:37

Japanese Tea Store

Reviews regarding cartoons mmorpg video game titles

Japanese Tea Store

September 7. 2010 00:30

computer repair westchester

We have to admit that here the content deserves a lot of congrats!

computer repair westchester

September 7. 2010 02:48

electronic cigarette

Come on guys, let's keep it clean. This is a good blog. Good info mate.

electronic cigarette

September 7. 2010 04:27

Carpet Cleaning Philadelphia

Hey, thanks for the article post. Great.

Carpet Cleaning Philadelphia

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading