Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does WCF have an equivalent of MVC's [Authorize] attribute?

Tags:

.net

wcf

I want to decorate certain Operation Contracts with an attribute to authorize the caller by custom logic, something like this:

[ServiceBehavior]
public class Service1
{
    [OperationContract]
    [Authorize] // ?? this should make sure only admins can call this method
    public List<SampleItem> GetCollection()
    {
        return new List<SampleItem>() { new SampleItem("Only Admins see me") };
    }
}

The [Authorize] should check if the caller is entitled to call this operation; if not - it should return an error fault.

Thanks.

like image 227
Dmitry Sadakov Avatar asked Jan 09 '11 20:01

Dmitry Sadakov


2 Answers

Not out of the box - but WCF top-guru Juval Löwy had a very interesting article in MSDN Magazine about Declarative WCF Security which goes in the same direction.

Juval identified several key security scenarios, and wrapped each of them up into a WCF service behavior to be applied as an attribute on your service class on the server side. Quite an interesting read indeed !

like image 184
marc_s Avatar answered Oct 15 '22 06:10

marc_s


WCF doesn't have any special attribute for this purpose but you can use PrincipalPermissionAttribute - common approach for declarative role-based security in .NET.

like image 32
Ladislav Mrnka Avatar answered Oct 15 '22 08:10

Ladislav Mrnka