我想要做同样的事情,在这个环节上:编程指定自定义授权WCF(NetTcpBinding的)
http://www.codeproject.com/KB/WCF/Custom_Authorization_WCF.aspx
但不使用配置文件。任何人都可以告诉我如何?
编辑:我想实现AuthorizationPolicy和CustomValidator。
我想要做同样的事情,在这个环节上:编程指定自定义授权WCF(NetTcpBinding的)
http://www.codeproject.com/KB/WCF/Custom_Authorization_WCF.aspx
但不使用配置文件。任何人都可以告诉我如何?
编辑:我想实现AuthorizationPolicy和CustomValidator。
你指的是如何通过代码添加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;
如果你指的是this topic (WCF Security: Getting the password of the user) by Rory Primrose,他发挥与你询问与提供自定义验证什么,重要的扩展方法是CreateSecurityTokenManager
:
public class PasswordServiceCredentials : ServiceCredentials
{
public PasswordServiceCredentials()
{
}
private PasswordServiceCredentials(PasswordServiceCredentials clone)
: base(clone)
{
}
protected override ServiceCredentials CloneCore()
{
return new PasswordServiceCredentials(this);
}
public override SecurityTokenManager CreateSecurityTokenManager()
{
// Check if the current validation mode is for custom username password validation
if (UserNameAuthentication.UserNamePasswordValidationMode == UserNamePasswordValidationMode.Custom)
{
return new PasswordSecurityTokenManager(this);
}
Trace.TraceWarning(Resources.CustomUserNamePasswordValidationNotEnabled);
return base.CreateSecurityTokenManager();
}
}
要使用此自定义服务的凭证,则需要在配置中指定的<ServiceCredentials>
的ConfigurationElement
类型的属性,如:
<serviceCredentials type="your.assembly.namespace.PasswordServiceCredentials,
your.assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
</serviceCredentials>
同样,你可以用编程方式设置这个type
属性,但我不太熟悉。
您没有正确阅读我的问题。请做。 – jgauffin 2010-09-01 12:42:22
对不起,但我已经通过配置扩展(即扩展元素)完成了我所有的WCF定制,但是从tomasr的领导开始,我相信他建议您可以通过在UserNamePasswordServiceCredential对象上设置以下属性来设置CustomValidator ServiceHostBase.Description.Behaviours.Find
那么这并不明确他推荐什么,我相信他建议你可以从头开始构建自己的服务凭证配置构造('ServiceCredentialsElement'),设置所有相关的属性(包括'UserNameAuthentication'),然后用任何'ServiceCredentials'已经通过构造“ServiceCredentialsElement.CreateBehaviour()”的行为而存在于所描述的服务主机行为中 - 尽管这是因为它在“BehaviorExtensionElement”上被声明为受保护的内部抽象对象CreateBehavior() – Rabid 2010-09-02 10:58:06
谢谢。你还可以展示如何用代码指定CustomValidator吗? – jgauffin 2010-07-11 13:25:37
再一次,我没有尝试过,但ServiceCredentials只是另一种服务行为,所以您应该能够创建ServiceCredentialsElement实例,设置UserNameAuthentication属性,然后调用CreateBehavior()并将其添加到ServiceHost。 – tomasr 2010-07-11 19:09:26