Can I configure Roles and Membership programmatically? Without an app.config (or web.config) ?
A:
From http://msdn.microsoft.com/en-us/library/5k850zwb.aspx
Roles.CreateRole("members");
Roles.CreateRole("manager");
Roles.AddUserToRole("JoeWorden", "manager");
string[] userGroup = new string[2];
userGroup[0] = "JillShrader";
userGroup[1] = "ShaiBassli";
Roles.AddUsersToRole(userGroup, "members");
Stick that in global.asax (application_start is probably your best bet) and Bob's your uncle.
roufamatic
2009-11-15 08:01:41
But with you answer I still need a web.config with something like:<roleManager enabled="true" cacheRolesInCookie="true" ></roleManager>
Nestor
2009-11-15 08:05:36
A:
It's kind of a hack, but here I configure Roles for Client Application Services:
ClientRoleProvider crp = new ClientRoleProvider();
// Initialize
NameValueCollection crp_config = new NameValueCollection();
crp_config.Add("serviceUri", "www.mydomain.com/Role_JSON_AppService.axd");
crp_config.Add("cacheTimeout", 5);
crp_config.Add("honorCookieExpiry", 300);
crp.Initialize("ClientRoleProvider", crp_config);
//RoleProviderCollection
RoleProviderCollection rpc = new RoleProviderCollection();
rpc.Add(crp);
rpc.SetReadOnly();
//Roles
BindingFlags enuBindingFlags = BindingFlags.NonPublic | BindingFlags.Static;
Type objRoleType = typeof(Roles);
objRoleType.GetField("s_Initialized", enuBindingFlags).SetValue(null, true);
objRoleType.GetField("s_InitializeException", enuBindingFlags).SetValue(null, null);
objRoleType.GetField("s_Enabled", enuBindingFlags).SetValue(null, true);
objRoleType.GetField("s_CookieName", enuBindingFlags).SetValue(null, ".ASPXROLES");
objRoleType.GetField("s_CacheRolesInCookie", enuBindingFlags).SetValue(null, false);
objRoleType.GetField("s_CookieTimeout", enuBindingFlags).SetValue(null, (int)30);
objRoleType.GetField("s_CookiePath", enuBindingFlags).SetValue(null, "/");
objRoleType.GetField("s_CookieRequireSSL", enuBindingFlags).SetValue(null, false);
objRoleType.GetField("s_CookieSlidingExpiration", enuBindingFlags).SetValue(null, true);
objRoleType.GetField("s_CookieProtection", enuBindingFlags).SetValue(null, CookieProtection.All);
objRoleType.GetField("s_Domain", enuBindingFlags).SetValue(null, null);
objRoleType.GetField("s_CreatePersistentCookie", enuBindingFlags).SetValue(null, false);
objRoleType.GetField("s_MaxCachedResults", enuBindingFlags).SetValue(null, (int)25);
objRoleType.GetField("s_Provider", enuBindingFlags).SetValue(null, crp);
objRoleType.GetField("s_Providers", enuBindingFlags).SetValue(null, rpc);
Nestor
2009-11-18 16:27:18