Get Started!

Authentication Check

Checking for authenticated status within a HTTPHandler

The example below is an expansion of the Hello World HTTP Handler Example

Much of the code below is boilerplate, the core of this handler is in the Handle method :

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

using Net4orce.Core;
using Net4orce.Core.Canvas;
using Net4orce.Core.Canvas.Meta;

using Newtonsoft.Json;

namespace Examples
{
  public class AuthedJSONHandler : HandlerBase, IRequiresSessionState 
  {

    public override void initialize(HttpContext context, AsyncCallback cb, object extraData)
    {
      base.initialize(context, cb, extraData);
      oOpperation = new Operation(cb, context, extraData);
    }

  }

  public class Operation : HandlerOpBase
  {
    public Operation(AsyncCallback cb, HttpContext context, Object state) 
              : base(cb, context, state) { }

    public override void Handle(object oExtraData) {

      // Test the Authed Status          
      if( !oSession.IsAuthed ) { 

        // Not Authed Stop proccessing
        NotAuthed();
        Close();
        return;
      }


      // Is Authenticated, Okay to continue 

      oResponse.ClearHeaders();

      oResponse.AddHeader("Content-Type", "text/plain");

      oResponse.Write(
        "Hello, " + oSession.oUser.cFirstName
      );

      Close();
    }
  }
}

The NotAuthed method is a built-in response that will render a HTML response stating that the user has insufficient rights.

Register the class

To register the handler use the follwoing, in your Application class :

CanvasApp.RegisterHandlerRoute(
  "auth-example",
  "handlers/example/json-auther.json",
  new AuthedJSONHandler()
);

Result / Response

The above example has been included & registered :

This is what's produced :

{
  "foo": "bar",
  "hello": "World",
  "obj": {
    "val": 321
  }
}