以编程方式为WCF指定自定义授权(NetTcpBinding)

jga*_*fin 7 .net c# wcf authorization

我想做与此链接相同的事情:

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

但是不使用配置文件.谁能告诉我怎么样?

编辑:我想实现AuthorizationPolicy和CustomValidator.

tom*_*asr 9

您是指如何通过代码添加AuthorizationPolicy?未经测试,但我相信这样的事情应该有效:

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;
Run Code Online (Sandbox Code Playgroud)