{"id":29,"date":"2005-09-29T15:53:05","date_gmt":"2005-09-29T20:53:05","guid":{"rendered":"http:\/\/stein.everybody.org\/brain\/net-get-roles-for-user\/"},"modified":"2011-03-10T10:57:34","modified_gmt":"2011-03-10T15:57:34","slug":"net-get-roles-for-user","status":"publish","type":"post","link":"https:\/\/jeremystein.com\/brain\/net-get-roles-for-user\/","title":{"rendered":".NET get roles for user"},"content":{"rendered":"<p>I searched a long time for a C# method to return the roles available for the logged-in user.<\/p>\n<p>I finally found <a href=\"http:\/\/www.15seconds.com\/issue\/050203.htm\"><code>private static string[] GetRoles(IPrincipal princ)<\/code><\/a> at 15seconds.com.<\/p>\n<p>To help Google, some things I searched for were:  C# windows roles, asp.net roles for a user<\/p>\n<p><!--\nIn case the link above is bad, here is the source:\nprivate static string[] GetRoles(IPrincipal princ)\n{\n   Type type = princ.GetType();\n\n   \/\/ Note: This code sets the 'MAGIC_NUMBER' field of the principal object.\n   FieldInfo field2 = type.GetField(\"MAGIC_NUMBER\", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);\n   field2.SetValue(princ,40); \/\/ This value can be any number but defaults to 23.\n\n   princ.IsInRole(\"DummyRole\"); \/\/ This call is required so that the subsystem goes and retrieves a list of roles.\n   \/\/ Without this call, the principal object does not contain any roles in its internal\n   \/\/ variables, and thus the code below that uses reflection to get the value of this variable\n   \/\/ will fail and return NULL.\n\n   FieldInfo field = type.GetField(\"m_roles\", BindingFlags.Instance | BindingFlags.NonPublic);\n   String[] roles = (String[]) field.GetValue(princ);\n\n   return roles;\n}\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I searched a long time for a C# method to return the roles available for the logged-in user. I finally found private static string[] GetRoles(IPrincipal princ) at 15seconds.com. To help Google, some things I searched for were: C# windows roles, asp.net roles for a user<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-net"],"_links":{"self":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":0,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"wp:attachment":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}