jwt token e vb net create code example

Example 1: JASON WEB TOKENS EM VBNET COM WINDOWS FORMS

{
  "iss": "JwtServer",
  "sub": "hrmanager",
  "email": "[email protected]",
  "jti": "e971bd9c-7655-41d5-9c49-fabc054dc466",
  "iat": 1503922683,
  "http://schemas.microsoft.com/ws/2008/06/identity/claims/role": [
    "Employee",
    "HR-Worker",
    "HR-Manager"
  ],
  "Department": [
    "HR",
    "HR"
  ],
  "nbf": 1503922683,
  "exp": 1503924483
}

Example 2: TOKEN JWT EXEMPLOS EM VBNET

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using JWT;
using Newtonsoft.Json.Linq;

namespace TimeworksAPI2
{
class Program
{
    const string AUTHSERVICE = "https://workingURL/";
    const string twpAIPURL = "https://workingURL/";

    static void Main(string[] args)
    {
        int PartnerID = 222;
        string SiteID = "XXXXX";
        string StartDate = "2017-04-11";
        string EndDate = "2017-04-11";
        string Category = "vacation";
        string key = "XXXXXXXXXXX";
        Dictionary<string, decimal> Results = GetAccrualData(
          StartDate,EndDate,Category, SiteID, key, PartnerID);

    }
    static public Dictionary<string, decimal> GetAccrualData(string StartDate, string EndDate, string Category, string SiteID, int PartnerID, string key)
    {

        var apptoken = "";
        var token = new
        {
            iss = PartnerID,
            product = "twppartner",
            sub = "partner",
            siteInfo = new
            {
                type = "id",
                id = SiteID
            },
            exp = (Int32)DateTime.UtcNow.Add(new TimeSpan(0, 4, 30)).Subtract(new DateTime(1970, 1, 1)).TotalSeconds
        };
        var jwt = JsonWebToken.Encode(token, key, JwtHashAlgorithm.HS256);
        WebRequest request = WebRequest.Create(AUTHSERVICE);
        request.ContentType = "application/json";
        request.Method = "POST";
        request.Headers.Set("Authorization", string.Format("Bearer {0}", jwt));
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.Created)
                             'QUALQUER COISA
     
     
'_________________________EM VB NET____________________________
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Net
Imports System.IO
Imports JWT
Imports Newtonsoft.Json.Linq

Namespace TimeworksAPI2
    Class Program
        Const AUTHSERVICE As String = "https://workingURL/"
        Const twpAIPURL As String = "https://workingURL/"

        Private Shared Sub Main(ByVal args As String())
            Dim PartnerID As Integer = 222
            Dim SiteID As String = "XXXXX"
            Dim StartDate As String = "2017-04-11"
            Dim EndDate As String = "2017-04-11"
            Dim Category As String = "vacation"
            Dim key As String = "XXXXXXXXXXX"
            Dim Results As Dictionary(Of String, Decimal) = GetAccrualData(StartDate, EndDate, Category, SiteID, key, PartnerID)
        End Sub

        Shared Public Function GetAccrualData(ByVal StartDate As String, ByVal EndDate As String, ByVal Category As String, ByVal SiteID As String, ByVal PartnerID As Integer, ByVal key As String) As Dictionary(Of String, Decimal)
            Dim apptoken = ""
            Dim token = New With {Key
                .iss = PartnerID, Key
                .product = "twppartner", Key
                .[sub] = "partner", Key
                .siteInfo = New With {Key
                    .type = "id", Key
                    .id = SiteID
                }, Key
                .exp = CInt(DateTime.UtcNow.Add(New TimeSpan(0, 4, 30)).Subtract(New DateTime(1970, 1, 1)).TotalSeconds)
            }
            Dim jwt = JsonWebToken.Encode(token, key, JwtHashAlgorithm.HS256)
            Dim request As WebRequest = WebRequest.Create(AUTHSERVICE)
            request.ContentType = "application/json"
            request.Method = "POST"
            request.Headers.[Set]("Authorization", String.Format("Bearer {0}", jwt))
            Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
            If response.StatusCode = HttpStatusCode.Created Then 
          
                                          'qualquer coisa 
           End If                             
        End Function
    End Class
End Namespace

Example 3: JWT EM VBNET

Dim PrivateKey As String = "MIIEowIBAAKCAQEAjtTe7UUP/CBI9s...BLABLABLA...JfwZ2hHqFPXA9ecbhc0".Replace(vbLf, "").Replace(vbCr, "")

Dim ar1 As JObject = New JObject()
ar1.Add("typ", "JWT")
ar1.Add("alg", "RS256")

Dim header As String = Base64UrlEncoder.Encode(ar1.ToString)

Dim ar2 As JObject = New JObject()
ar2.Add("iss", "INTEGRATION_ID")
ar2.Add("sub", "GUID_VERSION_OF_USER_ID")
ar2.Add("iat", DateDiff(DateInterval.Second, New Date(1970, 1, 1), Now().ToUniversalTime))
ar2.Add("exp", DateDiff(DateInterval.Second, New Date(1970, 1, 1), DateAdd(DateInterval.Hour, 1, Now().ToUniversalTime)))
ar2.Add("aud", "account-d.docusign.com")
ar2.Add("scope", "signature")

Dim body As String = Base64UrlEncoder.Encode(ar2.ToString)

Dim stringToSign As String = header & "." & body

Dim bytesToSign() As Byte = Encoding.UTF8.GetBytes(stringToSign)

Dim keyBytes() As Byte = Convert.FromBase64String(PrivateKey)

Dim privKeyObj = Asn1Object.FromByteArray(keyBytes)
Dim privStruct = RsaPrivateKeyStructure.GetInstance(privKeyObj)

Dim sig As ISigner = SignerUtilities.GetSigner("SHA256withRSA")

sig.Init(True, New RsaKeyParameters(True, privStruct.Modulus, privStruct.PrivateExponent))

sig.BlockUpdate(bytesToSign, 0, bytesToSign.Length)
Dim signature() As Byte = sig.GenerateSignature()

Dim sign As String = Base64UrlEncoder.Encode(signature)

Return header & "." & body & "." & sign

Example 4: JWT EM VBNET

Dim ac As ApiClient = New ApiClient()
Dim privateKeyStream() As Byte = Convert.FromBase64String(PrivateKey)
Dim tokenInfo As OAuth.OAuthToken = ac.RequestJWTUserToken("INTEGRATION_ID", "ACCOUNT_ID", "https://account-d.docusign.com/oauth/token", privateKeyStream, 1)

Example 5: JASON WEB TOKENS EM VBNET COM WINDOWS FORMS

var headerAndPayload = base64Encode(header) + "." + base64Encode(payload);

var secretkey = "@everone:KeepitSecret!";

signature = HMACSHA256(headerAndPayload, secretkey);