English 中文(简体)
How to configure Roles without an app.config?
原标题:

Can I configure Roles and Membership programmatically? Without an app.config (or web.config) ?

最佳回答

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);
问题回答

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.





相关问题
ASP.Net MVC - Showing Model data based on Roles

I have a View which needs to show and hide details based on the users role. I have 2 options using an inline if statement in the View to show and hide details Create multiple partial views and use ...

Help with roles in rails

Im trying to use this approach. My app is a cms. I have admins, editors, and subscribers. for a blog with categories... its an experimental project so I cant use wordpress or whatever. Admins should ...

Set Property Value on Master Page from Content Page

I tried following the advice posted here: Set Property Value on Master Page from Content Page. Specifically the last post about creating a class. However, visual studio keeps giving me an error on my ...

How to keep RoleProvider from overriding custom roles?

I have an custom role provider that gets the roles a user belongs to from a database. I also have a custom authentication module registered in my web.config s httpModules which sniffs incoming HTTP ...

Is this a good data model to implement strongly-typed roles?

Yes, this has been asked before here and here. I m curious as to whether the approach I m considering is architecturally sound. Let me start off by trying to describe what I d like to be able to do ...

SQL Server Database Roles via SMO

I am trying to add new roles to a SQL 2005 database via the SMO assemblies. The Roles.Add method just does not seem to add the new role. I have my user account set as securityadmin and sysadmin. ...

热门标签