Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically specify custom authorization for WCF (NetTcpBinding)

I want to do the same thing as in this link:

http://www.codeproject.com/KB/WCF/Custom_Authorization_WCF.aspx

But without using configuration files. Can anyone show me how?

Edit: I want to implement both AuthorizationPolicy and the CustomValidator.

like image 869
jgauffin Avatar asked Jul 11 '10 11:07

jgauffin


1 Answers

Are you referring about how to add the AuthorizationPolicy through code? Untested, but I believe something like this should work:

ServiceHost host = ...;
var col = new ReadOnlyCollection<IAuthorizationPolicy>(new IAuthorizationPolicy[] { new MyPolicy() });

ServiceAuthorizationBehavior sa = host.Description.Behaviors.Find<ServiceAuthorizationBehavior>();
if ( sa == null ) {
  sa = new ServiceAuthorizationBehavior();
  host.Description.Behaviors.Add(sa);
}
sa.ExternalAuthorizationPolicies = col;
like image 195
tomasr Avatar answered Oct 27 '22 01:10

tomasr