Search code examples
c#nancy

How to create the session and set the UserBean in session


I am new to Nancy and .NET framework. here I am running in a panic situation.

I have written my own Bootstrapper to enable CookieBasedSessions in project.Here are tasks i have to perform.

  1. Once user logged in create session and set User in session.
  2. Then on each request verify the user in session

     class Bootstrapper : DefaultNancyBootstrapper
    {
    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
    {
        base.ApplicationStartup(container, pipelines);
        CookieBasedSessions.Enable(pipelines);
    }
    
    }
    

Here is my controller.

     Post["/myapp/login"] = parameters =>
        {
            var model = new User();
            model.UserName = this.Request.Form.userName;


            model = manager.GetUser(model.UserName);
            if (!string.IsNullOrEmpty(model.UserName) && model.isAdmin())
            {
               // var user = Request.Session["user"];
                if (Request.Session["key"] != null)  // Here i get the ERROR
                    Request.Session["key"] = model;

                return Response.AsRedirect(HOME_URL);
            }
            else
                return Response.AsRedirect(LOGIN_URL);
        };

when I execute this code I am getting {"Session support is not enabled."} error.

Any help is appreciated.


Solution

  • It's not using your custom bootstrapper - make the class public and it will pick it up.