forked from zendesk/zendesk_jwt_sso_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_sharp_handler.cs
48 lines (39 loc) · 1.47 KB
/
c_sharp_handler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Handler: <%@ WebHandler Language="C#" Class="Zendesk.JWTLogin" CodeBehind="Zendesk.JWTLogin.cs" %>
// Requires: JWT (https://nuget.org/packages/JWT)
// Tested with .NET 4.5
using System;
using System.Web;
using System.Collections.Generic;
namespace Zendesk
{
public class JWTLogin : IHttpHandler
{
private const string SHARED_KEY = "{my zendesk token}";
private const string SUBDOMAIN = "{my zendesk subdomain}";
public void ProcessRequest(HttpContext context)
{
TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
int timestamp = (int) t.TotalSeconds;
var payload = new Dictionary<string, object>() {
{ "iat", timestamp },
{ "jti", System.Guid.NewGuid().ToString() }
// { "name", currentUser.name },
// { "email", currentUser.email }
};
string token = JWT.JsonWebToken.Encode(payload, SHARED_KEY, JWT.JwtHashAlgorithm.HS256);
string redirectUrl = "https://" + SUBDOMAIN + ".zendesk.com/access/jwt?jwt=" + token;
string returnTo = context.Request.QueryString["return_to"];
if(returnTo != null) {
redirectUrl += "&return_to=" + HttpUtility.UrlEncode(returnTo);
}
context.Response.Redirect(redirectUrl);
}
public bool IsReusable
{
get
{
return true;
}
}
}
}