Login and logout with Nancy

To manage login/logout on a Nancy application, it seems pretty simple, according to the existing documentation.

But all the examples I found assume that my navigation is manage server side, thus it works if you use return LoginAndRedirect() or LogoutAndRedirect()

Now let’s say you want to manage your navigation client side, with Sammy or anything else. In this case your do not want to use LoginAndRedirect() , so Nancy propose LoginWithoutRedirect().

But, still you may do not want to return directly the LoginWithoutRedirect()response, as you may want to include some custom data in your response.nancylogo-transparent

 

But the cookie to manage the Nancy session is set in this response, and won’t be set if you do not return this response. Thus basically what you have to do is to set the cookie by yourself.

Either clientside, as described here, or serverside (which I found more convenient).

On the serverside,  the trick is:

  • Get the cookie from the response from LoginWithoutRedirect()
  • Set this cookie into your own response
  • Same thing for LogoutWithoutRedirect()

In the end your login post function will look like that:

login

And your logout:

logout

Of course userfullName and statusCode are just Json example data, and you can put any custom data instead.

Hope it helps!

 

 

 
IP Blocking Protection is enabled by IP Address Blocker from LionScripts.com.