<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Piyush Shah's Blog : Security Group</title><link>http://blogs.msdn.com/shahpiyush/archive/tags/Security+Group/default.aspx</link><description>Tags: Security Group</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>How to check if a user belongs to a Security or Distribution group in ASP.Net</title><link>http://blogs.msdn.com/shahpiyush/archive/2009/03/25/how-to-check-if-a-user-belongs-to-a-security-or-distribution-group-in-asp-net.aspx</link><pubDate>Thu, 26 Mar 2009 03:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9509130</guid><dc:creator>shahpiyush</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/shahpiyush/comments/9509130.aspx</comments><wfw:commentRss>http://blogs.msdn.com/shahpiyush/commentrss.aspx?PostID=9509130</wfw:commentRss><description>&lt;P&gt;If you need to check at Runtime if your User belongs to a Security group. Here's how to do it - &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming the IIS Application has Authentication set to "Windows Authentication". The Request object of HttpContext has the Logged in users Identity which contains Groups. Here is the code which validates if a user belongs to a particular Security group - &lt;/P&gt;&lt;FONT size=2&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;P&gt;var&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt; account = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;&lt;FONT color=#2b91af size=2&gt;NTAccount&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#a31515 size=2&gt;&lt;FONT color=#a31515 size=2&gt;@"Domain\SecurityGroupName"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt;); &lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;FONT color=#008000 size=2&gt;//The security group you want to check the user belongs to&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;P&gt;var&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt; groups = &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;&lt;FONT color=#2b91af size=2&gt;HttpContext&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt;.Current.Request.LogonUserIdentity.Groups;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;FONT color=#008000 size=2&gt;//Get a collection of Groups the user belongs to&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;P&gt;bool&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt; hasAccount = groups.Contains(account.Translate(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;typeof&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;&lt;FONT color=#2b91af size=2&gt;SecurityIdentifier&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt;)));&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;FONT color=#008000 size=2&gt;//do the test&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;FONT color=#008000 size=2&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9509130" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/shahpiyush/archive/tags/ASP.Net/default.aspx">ASP.Net</category><category domain="http://blogs.msdn.com/shahpiyush/archive/tags/Security+Group/default.aspx">Security Group</category><category domain="http://blogs.msdn.com/shahpiyush/archive/tags/SecurityIdentifier/default.aspx">SecurityIdentifier</category><category domain="http://blogs.msdn.com/shahpiyush/archive/tags/NTAccount/default.aspx">NTAccount</category></item></channel></rss>