Add AuthorizationRuleData programmatically

Topics: Web Client Software Factory, Project Management Forum, UIP Application Block discussion, User Forum
Sep 6, 2007 at 12:12 AM
I need to add AuthorizationRuleData dynamically and not in the web.config. Does anyone know how to do this???

something like this:
but is only the rule and don't know how to add it?
IAuthorizationRule ruleData = new AuthorizationRuleData("TestRule","I:TestUser");

instead of like this:
<add expression="R:Administrator" name="AllowAdministerPermissions"/>
<add expression="R:Administrator OR R:Operator" name="AllowCreateTransfer"/>
<add expression="R:Administrator" name="AllowViewAccountsSummary"/>
<add expression="R:Administrator" name="AllowViewPaymentHistory"/>

thanks, tom
Sep 7, 2007 at 8:01 PM
hello does anybody out there know how to do this??????????
Sep 7, 2007 at 9:00 PM
I really don't think you're going to be able to do what you're thinking. You're probably going to have to write your own version of the AuthorizationRuleProvider because its authorizationRules collection isn't exposed and there are no methods provided to add new rules to it. The factory that creates it hands it the section from web.config that it needs and from then on it isn't changed, that I can see.

If you use .NET Reflector to look inside Microsoft.Practices.EnterpriseLibrary.Security.dll you can look at the AuthorizationRuleProvider class and see what all it does.
Sep 8, 2007 at 6:01 AM
I have tried to execute a code to load it from the database under the RegisterSiteMapInformation sub-rountine.

Follow data are loaded from the Database and. If you want to whole code, I could publish it to here later.

If Parent = -1 Then
Dim moduleNode As SiteMapNodeInfo = New SiteMapNodeInfo(Title, Url, Title, Description)
siteMapBuilderService.RootNode.Url = Url
siteMapBuilderService.RootNode.Title = Title
SiteMapNodeInfoObj.Add(ID, moduleNode)
Dim moduleNode As SiteMapNodeInfo = New SiteMapNodeInfo(Title, Url, Title, Description)
siteMapBuilderService.AddNode(moduleNode, DirectCast(SiteMapNodeInfoObj(Parent), SiteMapNodeInfo), Rules)
SiteMapNodeInfoObj.Add(ID, moduleNode)
End If

Sep 10, 2007 at 4:56 PM
Edited Sep 10, 2007 at 4:57 PM

After looking at the AuthorizationRuleProvider i thought maybe i could just override it with my own sub class. Then on my sub class constructor call the base constructor setting the IAuthorizationRule dictionary.

Sep 10, 2007 at 8:01 PM
Yeah, overriding it with a subclass is what I was talking about. If the methods in AuthorizationRuleProvider are accessible (protected, etc.) and they do what you want, then I'd say go for it. Otherwise, dig deeper till you find the abstract class they've built from and inherit from that and write the whole implementation yourself (this is recommended more often than not, a la the Provider model, Gang of Four book, etc.).
Sep 10, 2007 at 10:29 PM

well i was able to successfully create my own AuthorizationRuleProvider. Now i am not sure how to call it/register it???

the web.config says:
<add type="Microsoft.Practices.EnterpriseLibrary.Security.AuthorizationRuleProvider, Microsoft.Practices.EnterpriseLibrary.Security, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="RuleProvider">

when i change this too my new custom class i get errors.

any ideas???????

Sep 11, 2007 at 6:11 PM
It will depend on what the actual errors are that you're getting. Can you post them?
Sep 12, 2007 at 4:39 PM
no error just the custom rule provider i created never gets called.
Feb 1, 2008 at 8:45 AM
hi Tom,

can you please share your experience? as I am fighting with the same problem and still unable to find a way to manage those rules dynamically. have you any results? may be you can tell me how to overcome this.


tpagnotta wrote:
no error just the custom rule provider i created never gets called.

Aug 22, 2008 at 9:06 AM
Can anybody tell me how to read the rule infor from an XML file rather than the web.config file.
I want to store the rule information in a xml file  say,MyRules.xml or a database.
Plz some1 reply asap.

Thanks in advance.
Aug 22, 2008 at 6:53 PM



Perhaps you can find useful the following thread:

·         Custom Authentication And Roles


If you also want to have your Module View’s rules stored in a database or an XML file separated from the module’s web.config you will need to change the code that reads the rules from the web.config in the Configure method for something like this:


public override void Configure(IServiceCollection services, System.Configuration.Configuration moduleConfiguration)


    //TODO: Add the code to load your the modules's rules from your database/xml.


    IAuthorizationService authorizationRuleService = services.Get<IAuthorizationRulesService>();

    if (authorizationRuleService != null)


        foreach (CustomRule r in rules)


            authorizationRuleService.RegisterAuthorizationRule(r.AbsolutePath, r.RuleName);





Please, let me know if it helps.


Mariano Converti