Monday, July 22, 2019

[OpenLDAP] Create an OpenLDAP container


 ASP.NET Core   Identity Server 4   OpenLDAP  


▌Introduction


We are going to create an OpenLDAP Authentication Server by Identity Server 4.
The architecture is as following.



▌Environment


▋docker-openldap 1.2.4 (OpenLDAP 2.4.47)


▌Implement


▋Pull docker image and run the container

osixia/openldap: Docker Hub | Github


▋Pull

$ docker pull osixia/openldap:1.2.4


▋Standard OpenLDAP with user: admin/admin at domain: example.org

$ docker run -e -d  -p 389:389 -p 636:636 --name <container_name> osixia/openldap:1.2.4


▋Specify the password for admin                                                              

$ docker run -e LDAP_ADMIN_PASSWORD="<new_password>" -d  -p 389:389 -p 636:636 --name <container_name> osixia/openldap:1.2.4


▋Specify the domain

$ docker run -e LDAP_ORGANISATION="jb" --env LDAP_DOMAIN="jb.org" --env LDAP_ADMIN_PASSWORD="12qwaszx" -d  -p 389:389 -p 636:636 --name <container_name> osixia/openldap:1.2.4



▋Search OU in Container

$ docker exec <container_name> ldapsearch -x -H ldap://localhost -b dc=example,dc=org -D "cn=admin,dc=example,dc=org" -w admin

Result:





▋Manage OpenLDAP: LdapAdmin

Here is an example to create a new OU by LDAP tool: LdapAdmin.

First create a New connection,




Enter the following host settings. Notice that the Admin account is default:

Username: cn=admin,dc=example,dc=org
Password: admin



The LDAP shows after we connect to the host.
Right click on the DC and add a new User as below,




We can also set the password for the new user.



Now search the new user by

$ docker exec <container_name> ldapsearch -x -H ldap://localhost -b uid=jblin,dc=example,dc=org -D "cn=admin,dc=example,dc=org" -w admin

Result:  






▌Reference







Wednesday, July 17, 2019

[ASP.Net Core] Hybrid Filter


 ASP.NET Core   Filter  



▌Introduction


ASP.NET Core supports the Filter types:


And the lifecycle of them is as following (Diagram from Microsoft Docs)


Sometimes we would like to use the already-updated data from some of them.
Here is an example for logging the payload of request and the final HttpStatusCode of response.


▋Related articles





▌Environment


▋.NET Core 2.2.104



▌Implement


▋Hybrid filter by implement IActionFilter and IResultFilter

public class HybridFilter: IActionFilter, IResultFilter
{
        private readonly ILogger<LogFilter> _logger = null;
        private ICollection<object> payloads = null;

        public HybridFilter(ILogger<LogFilter> logger)
        {
            this._logger = logger;
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            this.payloads = context.ActionArguments == null ? null : context.ActionArguments.Values;
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
        }

        public void OnResultExecuting(ResultExecutingContext context)
        {
        }

        public void OnResultExecuted(ResultExecutedContext context)
        {
            var user = (User)this.payloads.FirstOrDefault();
            int httpStatusCode = context.HttpContext.Response.StatusCode;

            string msg = $"({httpStatusCode.ToString()}) {user.Name} signed in";
            this._logger.LogInformation(msg);
        }

}


Result:




▋Source code




▌Reference





Monday, July 8, 2019

[ASP.Net Core] Action Filter with Parameter(s)


 ASP.NET Core   Action Filter  



▌Introduction


Action filters can run code before and after an action method is called.
Sometimes we need to pass parameter(s) to the Action filter.
Here are 2 ways to make it happens:
1.  Inherit Attribute class and create Public prop(s) as the parameter(s) (See how to do it on ASP.NET frameworks)
2.  Use TypeFilter


▋Related articles



▌Environment


▋.NET Core 2.2.104



▌Implement


▋Inherit Attribute class and create Public prop(s)

▋Action filter

public class LogParamFilter : Attribute, IActionFilter
    {
        public EnumAction Action { get; set; }

        public void OnActionExecuted(ActionExecutedContext context)
        {
            string msg = $"[OnActionExecuted] Request for {this.Action.ToString()}";
            var logger = (ILogger<LogParamFilter>)context.HttpContext.RequestServices.GetService(typeof(ILogger<LogParamFilter>));
            logger.LogInformation(msg);
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            string msg = $"[OnActionExecuting] Request for {this.Action.ToString()}";
            var logger = (ILogger<LogParamFilter>)context.HttpContext.RequestServices.GetService(typeof(ILogger<LogParamFilter>));
            logger.LogInformation(msg);
        }
    }


▋Usage

[HttpGet("MyAction1")]
[LogParamFilter(Action = EnumAction.Action1)]
public async Task<IActionResult> MyAction1()
 {
     return Ok();
 }



▋Use TypeFilter

▋Action filter

public class LogFilter: IActionFilter
    {
        private readonly ILogger<LogFilter> _logger = null;
        private readonly EnumAction _action;

        public LogFilter(EnumAction action, ILogger<LogFilter> logger)
        {
            this._logger = logger;
            this._action = action;
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
            string msg = $"[OnActionExecuted] Request for {this._action.ToString()}";
            this._logger.LogInformation(msg);
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            string msg = $"[OnActionExecuting] Request for {this._action.ToString()}";
            this._logger.LogInformation(msg);
        }
    }

Notice that the constructor’s parameters include:

-   Incoming parameter(s), such as EnumAction action. Make sure passing the right number of incoming parameters, or give it a default value.

-   Injected service(s), such as ILogger<LogFilter> logger.

As you can see, TypeFilter is more continent to inject the services from DI container by constructor.



▋Usage

[HttpGet("MyAction2")]
[TypeFilter(typeof(LogFilter), Arguments = new object[] { EnumAction.Action2 })]
 public async Task<IActionResult> MyAction2()
 {
      return Ok();
 }




▋Result





▋Source code




▌Reference