Tip: Silverlight page authorization with the navigation framework
I came across a way to easily secure any Silverlight application, and thought I would share it with the list. It is a simple combination of 2 controls that can be found here http://www.davidpoll.com/2010/01/01/opening-up-silverlight-4-navigation-authenticationauthorization-in-an-inavigationcontentloader/
To secure your application you only need to put around 15 lines of xaml into MainPage.xaml
The syntax is similar to the page authorization config in an ASP.Net application
<authLoader:NavigationAuthRule UriPattern="^/Views/About\.xaml\??.*$"> <authLoader:Deny Users="?" /> <authLoader:Allow Users="*" /> </authLoader:NavigationAuthRule> <authLoader:NavigationAuthRule UriPattern="^/Views/RegisteredUsersPage.xaml\??.*$"> <authLoader:Allow Roles="Registered Users" /> </authLoader:NavigationAuthRule>
There are two controls:
- AuthContentLoader will check that the user is allowed to access the page. If they aren't then an UnauthorizedAccessException is thrown.
- To automatically redirect the user to a login screen, the AuthContentLoader control is wrapped in an ErrorPageLoader control. This is configured to catch the Unauthorized exception and redirect the user to the login screen
Hope this helps someone