O'Reilly logo

Mastering Internet of Things by Peter Waher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting a session token

We begin by creating a token factory. It will allow us to create tokens:

private JwtFactory tokenFactory = new JwtFactory(); 

We then add a resource that allows a web page, after having logged in, to get a session token:

this.httpServer.Register("/GetSessionToken", null, (req, resp) => 
{ 
   IUser User; 
 
   if (!req.Session.TryGetVariable("User", out Variable v) || 
          (User = v.ValueObject as IUser) == null) 
   { 
          throw new ForbiddenException(); 
   } 
 
   string Token = this.tokenFactory.Create( 
          new KeyValuePair<string, object>("sub", User.UserName)); 
 
   resp.ContentType = JwtCodec.ContentType; 
   resp.Write(Token); 
}, true, false, true); 
When you create a token, you provide a set of claims. One of the most important is sub, which means subject ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required