Given how easy it is to configure and mold Kentico to your needs, hosting a separate application inside of the platform is fairly simple process and can make life a lot easier for developers. Leveraging many of Kentico’s built-in features within your custom app is a great way to extend functionality without having to do a lot of heavy lifting. This blog details a few ways you can use Kentico functionality within your apps with just a little configuration.
NOTE
For these examples I’m assuming your custom app is a folder Kentico root and its code is compiled with the CMS app.
Security
One of the easiest ways to add security to your custom app is to use Kentico’s security model. Based on Forms authentication, with a few short lines of code you can secure your entire app and manage users from within Kentico. Let’s say our custom app has a master page (which it should for reasons just like this). In our master page, we can add the following code to check to make sure users are logged in in across the entire application.
//Check if the user is logged in.
if (CMSContext.CurrentUser.IsPublic())
{
//Who the heck is this guy? Beat it!
//Redirect to logon page...
}
With that section of code you can quickly ensure that all traffic to your custom app would require the user to be logged into Kentico beforehand.
Building on that example, you could add role or user level security checks to further secure you application.
if (!CMSContext.CurrentUser.IsInRole("[Your awesome role name]", "[Your Kentico site name]"))
{
//Send them packing....
//Redirecto Logon Pagifico!...
}
Using these techniques you can quickly add a security layer to your app without having to build the functionality by leveraging the Kentico API.
Logging Events
So you want to know when something happens within your custom app? Again, leveraging the Kentico API and the EventLogProvider to write to the Kentico Event log in 2 lines of code.
EventLogProvider ev = new EventLogProvider();
ev.LogEvent(EventLogProvider.EVENT_TYPE_INFORMATION, DateTime.Now, "[Custom Site Name]", "Execute", null, "[An extremely interesting description about what happened]");
Using this code in your custom app will allow you to add logging, debugging info, and just about anything you want to keep track of, all visible from within the Kentico Event Log.
And if by some amazingly unlikely chance that your custom code gets an error, you can write the exception to the Event Log and get notified via email (If you have that configured in the Site Manager) when it happens.
try
{
//Divide by zero and create a wormhole or something...
}
catch (Exception ex)
{
EventLogProvider.LogException("[Your exception-ridden, custom app name]", "Exception", ex);
}
User Info
If you are using the Kentico security API in your custom code you can then leverage the user account functionality of the platform. With this you can display the user name, profile pic, or any other user-based setting very simply.
<asp:Image ID="imgUser" runat="server" />
string strAvatar = "";
UserInfo user = UserInfoProvider.GetUserInfo(CMSContext.CurrentUser.UserID);
if (user.UserAvatarID > 0)
{
AvatarInfo ai = AvatarInfoProvider.GetAvatarInfo(user.UserAvatarID);
if (ai != null)
{
strAvatar = CMS.GlobalHelper.URLHelper.ResolveUrl("~/CMSModules/Avatars/CMSPages/GetAvatar.aspx") + "?avatarguid=" + ai.AvatarGUID.ToString() + "&maxsidesize=80";
}
}
imgUser.ImageUrl = strAvatar;
Leveraging the UserInfoProvider API within your app is a great way to add personalization without having to build up the infrastructure.
Logging some analytics
Another great use of the API is logging WebAnalytics within your custom code. This activity can be a page visit, a conversion, or any other action. Here’s an example that shows how to log a button click event into the Analytics for viewing within the reports.
<asp:Button id="btnLog" runat="server" Text="Add analytics hit" onclick="btnLog_Click" />
// Checks if web analytics are enabled in the settings
if (AnalyticsHelper.AnalyticsEnabled(CMSContext.CurrentSiteName))
{
// Adds a hit to a custom statistic
HitLogProvider.LogHit("buttonclicked", CMSContext.CurrentSiteName, null, CMSContext.CurrentUser.UserName, 0);
}
TIP
You can find a more detailed example of this in the Kentico CMS Developer’s guide
here.
Conclusion
We don’t regularly host an app within another site like this, however, there are always exceptions when it comes to custom development. Hopefully this blog sheds a little light on how you can leverage the Kentico API and all its functionality within your custom app and save yourself a ton of work!