{"id":144,"date":"2013-11-18T20:14:29","date_gmt":"2013-11-18T20:14:29","guid":{"rendered":"http:\/\/carla.romere.com\/?p=144"},"modified":"2013-11-18T20:14:29","modified_gmt":"2013-11-18T20:14:29","slug":"granting-permissions-based-on-ad-user-group-membership","status":"publish","type":"post","link":"https:\/\/carla.romere.com\/?p=144","title":{"rendered":"Granting Permissions Based on AD User Group Membership"},"content":{"rendered":"<p>After several different trial and error expeditions, I finally have a working PageLoad event to check whether the currently logged in user is a member of a specific group in Active Directory.<\/p>\n<p>The first step I used was a stored procedure to get a list of the user&#8217;s groups in Active Directory. We needed this for another project and I just reused it for this one.<\/p>\n<p><code><\/p>\n<p style=\"font-family: courier new; font-size: medium; color: blue;\">USE [master]<br \/>\nGO<br \/>\n\/****** Object: StoredProcedure [dbo].[GetLdapUserGroups] Script Date: 11\/18\/2013 11:58:14 ******\/<br \/>\nSET ANSI_NULLS ON<br \/>\nGO<br \/>\nSET QUOTED_IDENTIFIER ON<br \/>\nGO<br \/>\nCREATE PROCEDURE [dbo].[GetLdapUserGroups]<br \/>\n( @LdapUsername NVARCHAR(256) )<br \/>\nAS<br \/>\nBEGIN<br \/>\nDECLARE @Query NVARCHAR(1024), @Path NVARCHAR(1024)<br \/>\nSET @Query = 'SELECT @Path = distinguishedName FROM OPENQUERY(ADSI, '' SELECT distinguishedName FROM ''''LDAP:\/\/DC=YOURDOMAINHERE,DC=local'''' WHERE objectClass = ''''user'''' AND sAMAccountName = '''''<br \/>\n+ @LdapUsername + ''''' '') '<br \/>\nEXEC SP_EXECUTESQL @Query, N'@Path NVARCHAR(1024) OUTPUT', @Path = @Path OUTPUT<br \/>\nSET @Query = 'SELECT name AS LdapGroup FROM OPENQUERY(ADSI,'' SELECT name FROM ''''LDAP:\/\/DC=YOURDOMAINHERE,DC=local'''' WHERE objectClass=''''group'''' AND member='''''<br \/>\n+ @Path + ''''' '') ORDER BY name'<br \/>\nEXEC SP_EXECUTESQL @Query<br \/>\nEND<br \/>\nGO<\/p>\n<p><\/code><\/p>\n<p>The second step was I created another stored procedure that looks at the list of groups generated by the first stored procedure and sees whether the specific group being tested is in that list of groups.<br \/>\n<code><\/p>\n<p style=\"font-family: courier new; font-size: medium; color: blue;\">\nUSE [master]<br \/>\nGO<br \/>\n\/****** Object: StoredProcedure [dbo].[GetLdapUserGroups] Script Date: 11\/18\/2013 09:35:37 ******\/<br \/>\nSET ANSI_NULLS ON<br \/>\nGO<br \/>\nSET QUOTED_IDENTIFIER ON<br \/>\nGO<br \/>\nCREATE PROCEDURE [dbo].[aer_IsUserInGroup]<br \/>\n( @LdapUsername NVARCHAR(256), @GroupName NVARCHAR(50) )<br \/>\nAS<br \/>\nBEGIN<br \/>\nCREATE TABLE #usergroups (groups VARCHAR(50))<br \/>\nINSERT INTO #usergroups<br \/>\nEXEC dbo.GetLdapUserGroups @LdapUsername = @LdapUsername<br \/>\nSELECT CASE WHEN @GroupName IN(SELECT groups FROM #usergroups) THEN 1 ELSE 0 END<br \/>\nEND<\/p>\n<p><\/code><\/p>\n<p>The last step was putting the following in my PageLoad event of the page I want to protect based on the user being a member of a certain group.<\/p>\n<p><code><\/p>\n<p style=\"font-family: courier new; font-size: medium; color: blue;\">\nstring LoggedOnUser;<br \/>\nSystem.Type oType = System.Type.GetTypeFromProgID(\"Wscript.Network\");<br \/>\nobject pc = System.Activator.CreateInstance(oType);<br \/>\n\/\/Get NT userid data<br \/>\nLoggedOnUser = Request.ServerVariables[\"AUTH_USER\"];<br \/>\nint pos = LoggedOnUser.IndexOf('\\\\');<br \/>\nif (pos &gt; 0)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;LoggedOnUser = LoggedOnUser.Substring(pos + 1);<br \/>\n}<br \/>\nSqlCommand cmd = new SqlCommand();<br \/>\nSqlDataAdapter adp = new SqlDataAdapter();<br \/>\nint isingroup;<br \/>\nstring cs = null;<br \/>\ncs = \"Data Source=YOURSQLSERVER;Initial Catalog=master;User Id=USERID;Password=PASSWORD;\";<br \/>\nSqlConnection SQLCon = new SqlConnection(cs);<br \/>\ncmd = new System.Data.SqlClient.SqlCommand(\"aer_IsUserInGroup\", SQLCon);<br \/>\nSQLCon.Open();<br \/>\ncmd.CommandType = CommandType.StoredProcedure;<br \/>\nSystem.Data.SqlClient.SqlCommandBuilder.DeriveParameters(cmd);<br \/>\ncmd.Parameters[\"@LdapUsername\"].Value = LoggedOnUser;<br \/>\ncmd.Parameters[\"@GroupName\"].Value = \"msds\";<br \/>\n<span style=\"color: green;\">\/* \"msds\" is the AD Group of which the user must be a member *\/<\/span><br \/>\nadp.SelectCommand = cmd;<br \/>\nisingroup = Convert.ToInt16(cmd.ExecuteScalar());<br \/>\nSQLCon.Close();<br \/>\nif (isingroup == 1)<br \/>\n{<br \/>\nadmin.Visible = true;<br \/>\n}<\/p>\n<p><\/code><br \/>\nThe content I wanted to make sure was not accessible by people who were not in this group I put inside a <code><\/p>\n<p style=\"font-family: courier new; font-size: medium; color: blue;\">&lt;div id=\"admin\"&gt;Protected content here.&lt;\/div&gt;<\/p>\n<p><\/code> tag. I have tested with multiple users and multiple groups and it seems to be working so far. I will easily be able to plug this in to any page on which I want to limit a user&#8217;s functionality based on their AD groups membership.<\/p>\n<p>There are countless other ways to accomplish this, I&#8217;m sure, but this is the only one I was able to get to work rather easily.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After several different trial and error expeditions, I finally have a working PageLoad event to check whether the currently logged in user is a member of a specific group in Active Directory. The first step I used was a stored procedure to get a list of the user&#8217;s groups in Active Directory. We needed this for another project and I just reused it for this one. USE [master] GO \/****** Object: StoredProcedure [dbo].[GetLdapUserGroups] Script Date: 11\/18\/2013 11:58:14 ******\/ SET ANSI_NULLS&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/carla.romere.com\/?p=144\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[10,8,9],"class_list":["post-144","post","type-post","status-publish","format-standard","hentry","category-technology","tag-asp-net","tag-c","tag-tsql"],"_links":{"self":[{"href":"https:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/posts\/144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/carla.romere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=144"}],"version-history":[{"count":21,"href":"https:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":165,"href":"https:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/posts\/144\/revisions\/165"}],"wp:attachment":[{"href":"https:\/\/carla.romere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/carla.romere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/carla.romere.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}