March 2018
Beginner to intermediate
410 pages
10h 40m
English
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);
Read now
Unlock full access