CKFinder 3 – ASP.NET Connector Documentation
Configuration by Code

Note: This tutorial covers the basics of CKFinder initialization and does not explain how to set up more complex projects. For more detailed configuration options please refer to the classes reference.

This section describes how to configure the connector through code, which allows for dynamic changes to the configuration. If you prefer to configure the connector through static configuration files, please refer to Configuration.

Note: If CKFinder was installed using the ZIP package, you can define your dynamic configuration in the App_Data/ConnectorConfig.cs file. This file has a special purpose and does not require manual compilation — the configuration defined here will be compiled and applied during runtime.

Building the Connector

The CKFinder connector can be built with help of the ConnectorBuilder class.

The most important method of the ConnectorBuilder class is SetRequestConfiguration which defines the configuration for each request. This is the place where you add backends, resource types and ACL rules.

Note: A valid connector needs to have at least one backend, resource type and ACL rule.

Another method that needs to be invoked for correct connector setup is SetAuthenticator. CKFinder libraries do not provide any implementation of IAuthenticator. Please refer to Adding Authenticator for the description of an authenticator.

The basic setup may look like this:

1 public ConnectorBuilder ConfigureConnector()
2 {
3  var connectorBuilder = new ConnectorBuilder();
4  connectorBuilder
5  .SetRequestConfiguration(
6  (request, config) =>
7  {
8  config.AddProxyBackend("local", new LocalStorage(@"MyFiles"));
9  config.AddResourceType("Files", resourceBuilder => resourceBuilder.SetBackend("local", "files"));
10  config.AddAclRule(new AclRule(
11  new StringMatcher("*"), new StringMatcher("/"), new StringMatcher("*"),
12  new Dictionary<Permission, PermissionType>
13  {
14  { Permission.FolderView, PermissionType.Allow },
15  { Permission.FolderCreate, PermissionType.Allow },
16  { Permission.FolderRename, PermissionType.Allow },
17  { Permission.FolderDelete, PermissionType.Allow },
18 
19  { Permission.FileView, PermissionType.Allow },
20  { Permission.FileCreate, PermissionType.Allow },
21  { Permission.FileRename, PermissionType.Allow },
22  { Permission.FileDelete, PermissionType.Allow },
23 
24  { Permission.ImageResize, PermissionType.Allow },
25  { Permission.ImageResizeCustom, PermissionType.Allow }
26  }));
27  })
28  .SetAuthenticator(new MyAuthenticator());
29 
30  return connectorBuilder;
31 }

This setup adds the following items for each request:

  • A backend called local with a LocalStorage adapter.
  • A resource type named Files with the local backend and the files root directory.
  • An ACL rule that gives all permissions to everybody for all resource types and all paths.

Additionally, the example adds MyAuthenticator as an authenticator for all requests.

Adding Authenticator

CKFinder needs to know its users. You need to implement the IAuthenticator interface to inform CKFinder which users are allowed to access the interface and what roles they have.

A very simple implementation that grants access to all users and does not define any roles may look like this:

1 public class MyAuthenticator : IAuthenticator
2 {
3  public Task<IUser> AuthenticateAsync(ICommandRequest commandRequest, CancellationToken cancellationToken)
4  {
5  var user = new User(true, null);
6  return Task.FromResult((IUser)user);
7  }
8 }

You may use ICommandRequest.Principal in the implementation of this interface to get access to the principal in the current request. The AuthenticateAsync method is asynchronous, so it is safe to do IO operations inside. However, keep in mind that this method is called for every request and may have impact on overall CKFinder performance.

Final Steps

When you have defined your connectorBuilder it is time to build the connector and use it in your pipeline. The Build method accepts an implementation of IConnectorFactory. Currently CKFinder libraries provide only one connector factory: OwinConnectorFactory for use with Owin pipeline.

The simplest implementation of the Configuration method for your Owin's Startup class may look like this:

1 public void Configuration(IAppBuilder appBuilder)
2 {
3  var connectorBuilder = ConfigureConnector();
4  var connector = connectorBuilder.Build(new OwinConnectorFactory());
5  appBuilder.Map("/CKFinder/connector", builder => builder.UseConnector(connector));
6 }

Note: The mapped path (/CKFinder/connector) is where the JavaScript part of CKFinder expects to find the connector. If you want to map the connector under a different route, keep it in sync with the CKFinder JavaScript client connectorPath.

Self-hosted Application Example

Prerequisites

  1. Create an empty Console Application project with .NET framework 4.5 or newer.
  2. Install the following NuGet packages:

Code

Replace your Program.cs code with the following:

1 namespace Example
2 {
3  using System;
4  using System.Collections.Generic;
5  using System.Threading;
6  using System.Threading.Tasks;
7 
8  using CKSource.CKFinder.Connector.Core;
9  using CKSource.CKFinder.Connector.Core.Acl;
10  using CKSource.CKFinder.Connector.Core.Authentication;
11  using CKSource.CKFinder.Connector.Core.Builders;
12  using CKSource.CKFinder.Connector.Host.Owin;
13  using CKSource.FileSystem.Local;
14 
15  using Microsoft.Owin.FileSystems;
16  using Microsoft.Owin.Hosting;
17  using Microsoft.Owin.StaticFiles;
18  using Microsoft.Owin.StaticFiles.ContentTypes;
19 
20  using Owin;
21 
22  public class Program
23  {
24  /*
25  * Custom content provider with JSON support.
26  */
27  private class MyContentTypeProvider : FileExtensionContentTypeProvider
28  {
29  public MyContentTypeProvider()
30  {
31  Mappings.Add(".json", "application/json");
32  }
33  }
34 
35  /*
36  * Simple authenticator that allows access to all users and does not define any roles.
37  */
38  private class MyAuthenticator : IAuthenticator
39  {
40  public Task<IUser> AuthenticateAsync(ICommandRequest commandRequest, CancellationToken cancellationToken)
41  {
42  var user = new User(true, null);
43  return Task.FromResult((IUser)user);
44  }
45  }
46 
47  /*
48  * Application entry point.
49  */
50  public static void Main(string[] args)
51  {
52  var program = new Program();
53 
54  /* Run application under the http://localhost:9000 path. */
55  WebApp.Start("http://localhost:9000", program.Configuration);
56 
57  Console.ReadKey();
58  }
59 
60  /*
61  * Simple configuration of the connector.
62  */
63  public ConnectorBuilder ConfigureConnector()
64  {
65  var connectorBuilder = new ConnectorBuilder();
66 
67  connectorBuilder
68  .SetRequestConfiguration(
69  (request, config) =>
70  {
71  /* Add a local backend. */
72  config.AddProxyBackend("local", new LocalStorage(@"MyFiles"));
73 
74  /* Add a resource type that uses the local backend. */
75  config.AddResourceType("Files", resourceBuilder => resourceBuilder.SetBackend("local", "files"));
76 
77  /* Give full access to all resource types at any path for all users. */
78  config.AddAclRule(new AclRule(
79  new StringMatcher("*"), new StringMatcher("/"), new StringMatcher("*"),
80  new Dictionary<Permission, PermissionType>
81  {
82  { Permission.FolderView, PermissionType.Allow },
83  { Permission.FolderCreate, PermissionType.Allow },
84  { Permission.FolderRename, PermissionType.Allow },
85  { Permission.FolderDelete, PermissionType.Allow },
86 
87  { Permission.FileView, PermissionType.Allow },
88  { Permission.FileCreate, PermissionType.Allow },
89  { Permission.FileRename, PermissionType.Allow },
90  { Permission.FileDelete, PermissionType.Allow },
91 
92  { Permission.ImageResize, PermissionType.Allow },
93  { Permission.ImageResizeCustom, PermissionType.Allow }
94  }));
95  });
96 
97  /* Set the authenticator. */
98  connectorBuilder.SetAuthenticator(new MyAuthenticator());
99 
100  return connectorBuilder;
101  }
102 
103  /*
104  * Owin configuration.
105  */
106  public void Configuration(IAppBuilder appBuilder)
107  {
108  /* Configure the connector builder. */
109  var connectorBuilder = ConfigureConnector();
110 
111  /* Build connector middleware. */
112  var connector = connectorBuilder.Build(new OwinConnectorFactory());
113 
114  /* Map connector middleware to the /CKFinder/connector route. */
115  appBuilder.Map("/CKFinder/connector", builder => builder.UseConnector(connector));
116 
117  /* Configure access to local files for JavaScript files. */
118  var options = new FileServerOptions();
119  options.StaticFileOptions.ContentTypeProvider = new MyContentTypeProvider();
120  options.FileSystem = new PhysicalFileSystem("../../CKFinderScripts");
121 
122  /* Map local files at the root path. */
123  appBuilder.UseFileServer(options);
124  }
125  }
126 }

Note: This example runs at the http://localhost:9000 path. You may need to have elevated privileges or reserve this path for your user using the netsh add useracl shell command.