2010-07-11 6 views

답변

8

코드를 통해 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; 
+1

감사합니다. 또한 코드로 CustomValidator를 지정하는 방법을 보여줄 수 있습니까? – jgauffin

+0

다시 시도했지만 ServiceCredentials는 다른 서비스 동작이므로 ServiceCredentialsElement의 인스턴스를 만들고 UserNameAuthentication 속성을 설정 한 다음 CreateBehavior()를 호출하고 ServiceHost에 추가 할 수 있어야합니다. – tomasr

0

this topic (WCF Security: Getting the password of the user) by Rory Primrose을 참조하면, 그는 중요한 확장 방법은 CreateSecurityTokenManager 인 당신이 사용자 정의 유효성 검사기를 제공에 대해 문의하고 유사 달성 :

<serviceCredentials type="your.assembly.namespace.PasswordServiceCredentials, 
    your.assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" > 
</serviceCredentials> 
01 :

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에 type 속성을 지정해야합니다이 사용자 정의 서비스 자격 증명을 사용하려면

마찬가지로이 type 속성을 프로그래밍 방식으로 설정할 수 있지만 방법에 익숙하지 않습니다.

+1

내 질문을 제대로 읽지 못했습니다. 제발. – jgauffin

+0

죄송하지만, 구성 확장 (ie 확장 요소)을 통해 WCF의 모든 사용자 정의를 완료했습니다. 그러나 tomasr의 리드를 따르면서, 다음을 사용하여 액세스 된 'UserNamePasswordServiceCredential' 객체에서 다음 특성을 설정하여 CustomValidator를 설정할 수 있다고 제안합니다. (UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom' 및'UserNamePasswordValidator = new MyCustomValidator()')를 호출합니다. 희망이 도움이! – Rabid

+1

그가 추천하는 것을 명시 적으로 밝히지는 않았지만 처음부터 자신의 서비스 자격 증명 구성을 구축하고 ('ServiceName', 'UserNameAuthentication'을 포함하여) 관련된 모든 속성을 설정 한 다음 'ServiceCredentials' 'ServiceCredentialsElement.CreateBehaviour()'를 통해 동작을 생성함으로써 기술 된 서비스 호스트 동작 내에 이미 존재합니다 - BehaviorExtensionElement'에서'protected internal abstract Object CreateBehavior()'로 선언 되었기 때문에 직설적이지는 않습니다. – Rabid