John Miner
John Miner

Reputation: 893

How to prevent 'Request for principal permission failed' from faulting WCF client?

I have a WCF service with several operation. Each operation has the 'PrincipalPermission' tag, something like this :

[PrincipalPermission(SecurityAction.Demand, Role = "Administrator")]
public ProductsDto GetAllProducts()
{
     // Do operation here ...
}

Problem is - if the user is not part of the role 'Administratir' - the service throws the exception

Request for principal permission failed

This corrupts the client's channel to a 'Faulted' state.

I want to be able to catch this somehow and send the client a 'Fault' message,

so that the client knows he tried to do something he shouldn't, without faulting the channel !

I tried using a 'try-catch' block inside the operation, but it didn't help. The exception occurs 'outside' the operation itself.

How can I solve this ?

Upvotes: 1

Views: 2018

Answers (1)

Anders Abel
Anders Abel

Reputation: 69260

There's a special interface IErrorHandler that you can implement and hook up with the WCF service to handle the exception. Using IErrorHandler will let you handle both the security exceptions and any exceptions thrown by the serialization code.

Another note on the client's channel getting into a faulted state: I'd recomment creating a new client object for each unit of work you perform from the client. Reusing the client object opens up for this kinds of problems.

Upvotes: 1

Related Questions