Saturday 28 November 2015

Configuring Windsor Castle with Web API and MVC

Add below to the Application_Start() in Global.asax.cs
 
WindsorActivationConfig.Setup(GlobalConfiguration.Configuration);
 

public static class WindsorActivationConfig
    {
        public static void Setup(HttpConfiguration config)
        {
            IWindsorContainer container = new WindsorContainer();

            container.Register(Classes.FromThisAssembly().BasedOn().LifestyleTransient());

            config.Services.Replace(typeof(IHttpControllerActivator), new WindsorControllerActivator(container));

           // register your components here now
        }
    }

 
public class WindsorControllerActivator : IHttpControllerActivator
    {
        private readonly IWindsorContainer _container;

        public WindsorControllerActivator(IWindsorContainer container)
        {
            this._container = container;
        } 
        
        public IHttpController Create(HttpRequestMessage request,
            HttpControllerDescriptor controllerDescriptor,
            Type controllerType)
        {
            var controller = (IHttpController) this._container.Resolve(controllerType);
            request.RegisterForDispose(new Release(() => this._container.Release(controller))); 
            return controller;
        }
        
        private class Release : IDisposable
        {
            private readonly Action _release;

            public Release(Action release)
            {
                this._release = release;
            } 
            public void Dispose()
            {
                this._release();
            }
        }
    }

  
public abstract class DependenciesInstallerBase
    {
        protected readonly WindsorContainer Container;

        protected DependenciesInstallerBase(WindsorContainer container, bool enableArrayInjection = true)
        {
            Container = container;

            if (enableArrayInjection)
            {
                // enables injection of interface arrays to a registered object
                Container.Kernel.Resolver.AddSubResolver(new CollectionResolver(Container.Kernel));
            }
        }
        
        protected void RegisterAllDefaultInterfacesWithinAssembly()
        {
            // registers objects with their interfaces
            // registers multiple objects for the same interface
            Container.Register(Classes
                .FromAssembly(typeof(T).Assembly)                
                .BasedOn()
                .WithService.AllInterfaces()         
                .LifestyleTransient());            
        }
    }

Thursday 10 September 2015

Windows Remote Management Service


Check Remote Access
winrm get winrm/config -r:HOSTNAME

Set Trusted Hosts on a Remote Server
winrm set winrm/config/client '@{TrustedHosts="machineA,machineB"}'
This means that machineA and machineB are trusted to remove to the Remote Server.

Create a Permanent Connection to a Remote Server
New-PSSession -ComputerName HOSTNAME

Create a Permanent Connection to a Remote Server via a Port Number and a User Credential
New-PSSession -ComputerName HOSTNAME -port 81 -cred DOMAIN\USERNAME

Open Port 81 on a Server
set-item wsman:\localhost\listener\listener*\port -value 81 -force

See the Listener Details
winrm enumerate winrm/config/listener


Remove WRM Listener Configurations
winrm invoke Restore winrm/Config

Enable PowerShell Remoting
Enable-PSRemoting

Restart the Remote Manangement service
net stop winrm
net start winrm

Set Listener Port
winrm set winrm/config/Listener?Address=*+Transport=HTTP '@{Port="5985"}'

Tuesday 13 January 2015

How to configure IP Address Restriction for your MVC/WebForms Application?

Step 1: Enable IP Security Feature in Windows

  • Go to "Turn Windows Features on or off"
  • Find the IP Security checkbox under IIS/WorldWide Services/Security
  • Enable it
Step 2: Unlock IP Security Feature in IIS
  • Open IIS Manager
  • Click the Server name/Configuration Editor
  • Select IP Security section
  • Make sure it’s unlocked
Step 3: Add the Restrictions to the Web.config

Add only the IP Addresses that must be allowed access to this:

“allowUnlisted” attribute means access to all IP Addresses must be denied apart from the ones listed below.

“allowed” attribute must be set to true otherwise by default it’s false and just denies access to the IP Address.

http://blogs.endjin.com/2014/09/restrict-access-to-azure-websites-by-whitelisting/