Programmatically specify custom authorization for WCF (NetTcpBinding)?

Untested, but I believe something like this should work.

Up vote 4 down vote favorite 4 share g+ share fb share tw.

I want to do the same thing as in this link: codeproject.com/KB/WCF/Custom_Authorizat... But without using configuration files. Can anyone show me how? Edit: I want to implement both AuthorizationPolicy and the CustomValidator.

C# .net wcf authorization link|improve this question edited Jul 11 '10 at 13:25 asked Jul 11 '10 at 11:29jgauffin27k42976 87% accept rate.

Untested, but I believe something like this should work: ServiceHost host = ...; var col = new ReadOnlyCollection(new IAuthorizationPolicy { new MyPolicy() }); ServiceAuthorizationBehavior sa = host.Description.Behaviors.Find(); if ( sa == null ) { sa = new ServiceAuthorizationBehavior(); host.Description.Behaviors. Add(sa); } sa. ExternalAuthorizationPolicies = col.

1 Thanks. Can you also show how I specify the CustomValidator with code? – jgauffin Jul 11 '10 at 13:25 Again, I haven't tried it, but ServiceCredentials is just another service behavior, so you should be able to just create an instance of ServiceCredentialsElement, set the UserNameAuthentication properties, then call CreateBehavior() on it and add to the ServiceHost.

– tomasr Jul 11 '10 at 19:09.

If you refer to the this topic (WCF Security: Getting the password of the user) by Rory Primrose, he achieves similar to what you're enquiring about with providing a custom validator, the important extension method being 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(); } } To use this custom service credential, you will need to specify the type attribute on the 's ConfigurationElement in your configuration, like: Likewise, you could set this type attribute programatically, but I'm not familiar with how.

You didn't read my question properly. Please do. – jgauffin Sep 1 '10 at 12:42 Sorry, but I've done all my customisation of WCF through configuration extensions (i.e.

Extension elements), but following from tomasr's lead, I believe he's suggesting you can set your CustomValidator by setting the following properties on the UserNamePasswordServiceCredential object accessed using ServiceHostBase.Description.Behaviours.Find(). UserNameAuthe? Ntication: UserNamePasswordValidationMode = UserNamePasswordValidationMode.

Custom and UserNamePasswordValidator = new MyCustomValidator(). Hope this helps! – Rabid Sep 2 '10 at 10:48 Well thats not explicitly what he's recommending, I believe he's suggesting that you could build your own service credentials configuration construct from scratch (ServiceCredentialsElement), set all the relevant properties (including the UserNameAuthentication), then substitute any ServiceCredentials already present within the described service host behaviours by constructing the behaviour via ServiceCredentialsElement.CreateBehaviour() - not straight forward though because it is declared as protected internal abstract Object CreateBehavior() on BehaviorExtensionElement – Rabid Sep 2 '10 at 10:58.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions