Try and Catch
Security Application Block
Just Like my previous post, i wrote this in MS.Word and the picture is not show when i pasted it to this pane..[:'(], I have attach file
The Security Application Block
was created to simplify and standardized the way that applications authorized
and cache security information within an application.
Look and Feel Security Application Block Design
If we look closely Security
Application Block was design with flexibility to implement different security
issue in an application and also provide most standard security implementation.
Security Application Block profiles the user information that could be used in
form of context.
Figure SEQ Figure \* ARABIC 25 Authorization Object Model
Figure SEQ Figure \* ARABIC 26 SecurityCache Object Model
Let’s examine the each of the
class that involve in the object model.
The AuthorizationFactory is
the class that you could get the authorization provider. There are
two implementations of the provider AzManAuthorizationProvider and AuthorizationRuleProvider.
AzMan (Authorization Manager) provider provide standard interface to maintain
Active Directory, roles groups, tasks and operations. The
AzManAuthorizationProvider provide functionality that easy determine the
authorization from the user identity information. On the other hand
AuthorizationRuleProvider determine authorization with defined rule for each
task or operation.
Figure SEQ Figure \* ARABIC 27
AuthorizationFactory
Figure SEQ Figure \* ARABIC 28 AzManAuthorizationProvider
Figure SEQ Figure \* ARABIC 29 AuthorizationRuleProvider
Authorization need to use
information to authenticated user with their profile. This information
basically store in stateless storage. The user should be verified whenever one
or more requests have been made to the server or application. These processes
could hit the performance. Security
Application Block will help this problem with solution via cache. The Security
Application Block cache the user profile so it could be use throughout
application.
Figure SEQ Figure \* ARABIC 30 SecurityCacheFactory
Like AuthorizationFactory
Security Application Block provide Factory design pattern to get Cache
Store Provider via SecurityCacheFactory.
Figure SEQ Figure \* ARABIC 31 CachingStoreProvider
The CachingStoreProvider
responsibilities to return user profile information. It could return user
identity and principal that has been defined in their profile.
Unlock Security Application Block
I will show you how to implementing
Rule Provider using AuthorizationRuleProvider.
1. First
create application configuration.
2. Edit
the application with Enterprise Library Configuration Editor.
3. Add
Security Application Block to the configuration
Action ::
App Config Root >> New >> Security Application Block
You could
only create one Security Application Block per application level.
4. Creating
Authorization Rule Provider, by right click the Authorization node.
Action ::
Authorization >> New >> Authorization Rule Provider
This will
create Rule Provider Node. You could rename the rule provider. Let’s say you
rename it to “MyTestRuleProvider”. You
could have more than one Authorization Rule Provider in the application.
5. Next
Add Rule to the Rule Provider that has been created before.
Action ::
Rule Provider >> New >> Rule
Rename the
rule to “MyTestRule”.
6. Each
Rule has expression that defines the rule algorithm. Define the rule expression
by click the ellipsis button in the expression property. This action will show us Rule Expression
Editor.
Figure SEQ Figure \* ARABIC 32 Rule
Expression Editor
In the Rule
Expression Editor there are many buttons that you could use to create
expression for the rule. The rule expression logic defines Identity and
Principle that will be authenticated. Let’s assume we define that only Identity
of henry, ela and role Manager that should be allowed other will denied. We
could test the expression with given value.
7. Close
the Enterprise Library Configuration Editor. You will see that Enterprise
Library Configuration Editor will update the application configuration.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section
name="securityConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Security.Configuration.
SecuritySettings,
Microsoft.Practices.EnterpriseLibrary.Security,
Version=3.1.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" />
<section
name="dataConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.
DatabaseSettings,
Microsoft.Practices.EnterpriseLibrary.Data,
Version=3.1.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" />
</configSections>
<securityConfiguration
defaultAuthorizationInstance=""
defaultSecurityCacheInstance="">
<authorizationProviders>
<add
type="Microsoft.Practices.EnterpriseLibrary.Security.
AuthorizationRuleProvider,
Microsoft.Practices.EnterpriseLibrary.Security,
Version=3.1.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
name="MyTestRuleProvider">
<rules>
<add
expression="(I:henry OR I:ela) AND
R:Manager "
name="MyTestRule" />
</rules>
</add>
</authorizationProviders>
</securityConfiguration>
</configuration>
Figure SEQ Figure \* ARABIC 33 Enterprise Library Configuration Editor
8. After
finishing the configuration let create code that consume the configuration
class.
You will
need to add two references in this code.
o
System.Security.dll
This will provide Identity and Principal object.
o
Microsoft.Practices.EnterpriseLibrary.Security.dll
This will provide Authorization Provider.
This the code snippet
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Principal;
using Microsoft.Practices.EnterpriseLibrary.Security;
namespace UsingSecurityApplicationBlock
{
class
Program
{
static
void Main(string[]
args)
{
TestAuthorization();
}
public
static void
TestAuthorization()
{
IPrincipal
principle = new GenericPrincipal(
new
GenericIdentity("henry"),
new string[]
{ "Manager" });
IAuthorizationProvider
provider =
AuthorizationFactory.GetAuthorizationProvider(
"MyTestRuleProvider");
bool
authorized = provider.Authorize(principle, "MyTestRule");
if
(authorized)
Console.WriteLine("You are authorized to use this...");
else
Console.WriteLine("You are not authorized to use this...");
Console.WriteLine("Press any key to exit!");
Console.ReadLine();
}
}
}
First we create principle
object that pass IIdentity and array of string role. Create instance of
IAuthorizationProvider pass the rule provider name. Call Authorize method to
invoke authentication process. This method returns bool value whether the
authentication failed or succeeded.
We have seen how to implement
Authorization using Security Application Block, let’s move to use cache with
Security Application Block.
Using Cache Store provider we
could retrieve and save information regarding user profile.
1. If
you follow the step of creating Rule Provider, you could edit the file by
Enterprise Library Editor too.
2. Create
Caching Store Provider
Action ::
Security Cache Node >> New >> Caching Store Provider
This action
will create Caching Store Provider node.
These node
have properties like this:
|
Property Name
|
Description
|
|
Name
|
Caching Store Provider name
|
|
AbsoluteExpiration
|
Number of
minutes in which the added object expires and is remove from the cache
|
|
CacheManager
|
|
Name
|
Description
|
|
ExpirationPollFrequencyInSeconds
|
Interval of checking expirations
|
|
MaximumElementsInCacheBeforeScavenging
|
Maximum
number of cache items stored before scavenging occurs
|
|
Name
|
Cache Manager name
|
|
NumberToRemoveWhenScavenging
|
Number
of cache items to remove when scavenging occurs
|
|
|
SlidingOperation
|
The number of minutes between the time added
object was last accessed and when object expires
|
Figure SEQ Figure \* ARABIC 34 Caching Store Provider Properties Definition
3. Define
the property like this screen shoot
Figure SEQ Figure \* ARABIC 35 Caching Store Provider Properties
4. You
will see your application configuration file has been updated like this
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section
name="securityConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Security.
Configuration.SecuritySettings,
Microsoft.Practices.EnterpriseLibrary.Security,
Version=3.1.0.0, C
ulture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" />
<section
name="dataConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Data.
Configuration.DatabaseSettings,
Microsoft.Practices.EnterpriseLibrary.Data,
Version=3.1.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" />
<section
name="cachingConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.
Caching.Configuration.CacheManagerSettings,
Microsoft.Practices.EnterpriseLibrary.Caching,
Version=3.1.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" />
</configSections>
<securityConfiguration
defaultAuthorizationInstance="" defaultSecurityCacheInstance="">
<authorizationProviders>
<add
type="Microsoft.Practices.EnterpriseLibrary.
Security.AuthorizationRuleProvider,
Microsoft.Practices.EnterpriseLibrary.Security,
Version=3.1.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
name="MyTestRuleProvider">
<rules>
<add
expression="(I:henry OR I:ela) AND
R:Manager "
name="MyTestRule" />
</rules>
</add>
</authorizationProviders>
<securityCacheProviders>
<add
cacheManagerInstanceName="MyCacheManager"
defaultSlidingSessionExpirationInMinutes="10"
defaultAbsoluteSessionExpirationInMinutes="60"
type="Microsoft.Practices.EnterpriseLibrary.
Security.Cache.CachingStore.CachingStoreProvider,
Microsoft.Practices.EnterpriseLibrary.
Security.Cache.CachingStore,
Version=3.1.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
name="MyCachingStoreProvider" />
</securityCacheProviders>
</securityConfiguration>
<cachingConfiguration
defaultCacheManager="MyCacheManager">
<cacheManagers>
<add
expirationPollFrequencyInSeconds="60"
maximumElementsInCacheBeforeScavenging="1000"
numberToRemoveWhenScavenging="10"
backingStoreName="Null Storage"
name<