Archive for December, 2008

Configurable PrincipalPermission attribute

2 Comments »

I while ago, a question came up in the WCF Forum about configuring the role and/or user name properties of the PrincipalPermission attribute. As I answered, it is possible to create a custom version of the attribute (deriving from the CodeAccessSecurityAttribute, since the PrincipalPermission attribute is sealed) and pull the property values from the {web|app}.config file.

I implemented a solution for this about a year ago and planned to put up a blog post about it, but it never made it out to the public (the main cause is probably that I experienced a blog-block period of my life :-P).

The same requirement may be a viable solution i system I’m currently working on for a customer, so I dug through my archives and found the old code.

I’ve polished it a bit made it available here under the Apache License 2.0.

The extended version, PrincipalPermissionEx can be used in two modes; either as a “normal” derivable PrincipalPermission attribute or an attribute that uses the configuration system (or a combination of both).

Instead of using the generic PrincipalPermission attribute, you’ll make derived version for each system role with a sensible name – making it more reliable and resistant to typos; e.g.

[MustHaveSuperUserPrivilegesPermission]
public void PrivilegedOperation(…)
{
}

instead of:

[PrincipalPermission(Role = "MYDOMAIN\SuperUsers")]
public void PrivilegedOperation(…)
{
}


Take a look at the supplied sample code to see how this is implemented.

The usage of PrincipalPermission-based authorization is useful in a variety of scenarios; it can be applied to WCF services, ASP.NET & Smart Client applications. Note that if you put the user name/role in the configuration file, you will need to ensure that the file is locked down with an appropriate ACL to prevent tampering by malicious users. This might not apply to solutions hosted on a locked down server (i.e. IIS-hosted web applications and services) but for smart / desktop clients where the user might have higher privileges to files on the local file system it is necessary to be aware of this.

As always, feedback is welcome :-)

kick it on DotNetKicks.com


LINQ to XML: XPathSelectElement Annoyance

No Comments »

It may be me – since I’m no XPath (or XSLT) pro, but the following is in my book a bug – or at least an annoyance category 3:

Given the following XML document loaded into an XDocument:

<?xml version="1.0" encoding="utf-8"?>
<Elements>
  <Element Id="1" />
  <Element Id="2" />
  <Element Id="3" />
  <Element Id="4" />
  <Element Id="5" />
</Elements>

The following XPath should  yield the first element of the list:

"//Element[@Id = '1']"


Guess what? If use the .XPathSelectElement() extension method, the result will be null – nada!

"//Element[@Id='1']"


The same query without the whitespace around the equal sign will give you the right result.

If you’re an XPath pro I would like your opinion on the matter – or else I’m turning this issue over to http://connect.microsoft.com/

Sigh.


Codename “Velocity” WF/WCF Persistence Provider

4 Comments »

So, it’s been a bit quiet here lately. The natural cause of it is (in no particular order):

  • A lot of work
  • Spending quality time with my son
  • Hacking on different kinds of technology bits (mainly pieces released at the PDC 2008)

I’ve also tried to get a clear picture of my “blind spots” when it comes to WCF. Even though I feel quite competent, there are still tons of stuff that I don’t touch daily so I still have to “rehearse”.

Since I have “Get to know Workflow Foundation – for real” on my TODO list I spent some time playing with durable services.

The persistence provider mechanism that is located in System.WorkflowServices is not exclusive to to Workflows / Workflow services. It can also be used with “vanilla” WCF Services.

The idea is that the framework can persist the service instance after you have invoked a method and when a future method invocation comes down the wire, it can pull it from the persistence store – revive it and pass the call to the “same” instance. A perfect fit for the scenario of long running services.

So how do you enable durable services? It is quite easy. First, you decorate your service implementation with [DurableService] and one of the mechanisms that specifies that the type is serializable (I chose [Serializable] for the sake of simplicity).

image

In this code snippet we also see that there is another attribute that can be used to tell the persistence mechanism that a call to an operation creates the instance or tears it down; [DurableOperation].

The next thing you have to do is to wire up a persistence provider using either configuration or programmatically.

Out of the box there exists only one Persistence Provider; One suited for persisting the service instances to SQL Server – System.ServiceModel.Persistence.SqlPersistenceProviderFactory. You will have to set up a SQL Server Database instance with the schema located in C:\Windows\Microsoft.NET\Framework\v3.5\SQL\EN.

But that was a digression – now back to my custom “Velocity” Persistence Provider. If you don’t know what Codename “Velocity” is, I suggest that you head over here and read more about it. The short description:

It is Microsoft’s attempt to create an in-memory, high-performance, distributed caching supporting different scenarios that can suite many needs in both a web farm or other places where caching is needed. The current version that is publicly available is CTP2. We should expect a new CTP in March (around the time of MIX’09) and the RTW/RTM in the mid of 2009.

To implement a custom persistence provider, you will have to create two classes; the persistence provider implementation and its factory. It is the fully qualified type name of the factory that is specified when you set up the configuration.

The following configuration snippet shows how a custom service behavior is set up. You will have to set the behaviorConfiguration attribute on the service element to “defaultServiceBehavior” in this case.

image

The code for the provider is available here (Licensed under the Apache License 2.0).

Cheers :-)
kick it on DotNetKicks.com


Zune Marketplace: Playlists – a Word of Warning

No Comments »

I’m still very satisfied with the 120GB black Zune that I bought during my trip to the Microsoft Professional Developers Conference 2008 in the end of October. I found a way to use my own Norwegian credit card with the combination of the regional settings in Windows set to “USA” and a borrowed, real address in … LA :-) I first signed up for the 14 day Zune Pass trial – and I was definitely sold.

Luckily, the tricks worked with the real Zune Pass subscription too – so now I’m in my first month of using the Zune Marketplace for for real. I’ve download a great deal of the music I own from before since I’m lazy and haven’t hooked up the external hard drive with the backup of all my music (I’m a recovering CD junkie, so my shelves are filled with albums) so it is a bit weird not having the physical media for some my newly-acquired music.

Here the other day some ads for “The very best of 199X” hit lists turned up on the front page of the Picks section of the Marketplace. Guess what, the summer of 1996 I was 18 and could legally get into pubs and drink beer, so there is a lot of the music from that year that awakes good memories. A good example is The Fugees, that had at least 3 or 4 big hits during the spring, summer and fall.

Back to the playlist. When I saw this “The very best of 1996” playlist I immediately added it to my Downloads list and went to bed. The morning after it was all sitting on my local disk, so I synchronized the Zune and head off for work.

At the train I picked up the Zune, plugged in my Bose QC2 headphones and navigated to the 1996 playlist.

Guess what?

My Zune was infested with crap. Crap. More crap. Unmentionable crap. Boring crap. Boyband crap. Dull drama crap. Crap.

Some example crap artist & songs:

  • Celine Dijon – err. Dion
  • Los del Rio – Macarena
  • Maxi Priest
  • Coolio
  • Barbra Streisand
  • Toni Braxton
  • Hanson

And the list just goes on and on and on and on.

And I forgot to mention that I also downloaded the list for 1997 & 98. sic.

So, what is the morale of this story?

DON’T – I REPEAT DON’T – DOWNLOAD PLAYLISTS FROM OTHER PEOPLE / BEST OF / HIT LISTS WITHOUT CHECKING THE CONTENT OF IT FIRST.

It really garbles the navigation system of the Zune so I have a major cleanup in front of me.

I kept the worst part to the last: Guess what? Since the fine piece of hardware (and the SW player too) keeps tracks of all the tunes you’ve played – the crap may show up on your Zune Tag and possibly affect the content that turns up in the Picks section.

Urgh. Sorry for bothering you with this rant – but the warning is well-meant. Have a nice weekend :-)