VB.Net使用JWT令牌调用C#.net函数

时间:2017-04-11 18:28:42

标签: c# vb.net jwt

首先,我对C#.NET知之甚少。我所拥有的是一个工作实例,我已经修改过以获得我需要的东西。试错。我所拥有的是一个有效的C#.net控制台应用程序;

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)
     ....

这符合代码中的要点;我获得了授权令牌。我接下来要做的是创建一个新的C#.NET项目,这次它是一个类库。我使用相同的代码(复制和粘贴)将库编译为DLL。然后我创建一个VB.NET控制台应用程序,引用C#dll:

Imports TWPAOI2Lib.TWPLIB

Module Module1


Sub Main()
    Dim sStartDate As String = "2017-04-11"
    Dim sEndDate As String = "2017-04-11"
    Dim sCategory As String = "vacation"
    Dim iPartnerID As Int32 = 222
    Dim sPartnerKey As String = "XXXXXXXX"
    Dim sSiteID As String = "XXXX"

    Dim AccrualData As Dictionary(Of String, Decimal) = GetAccrualData(sStartDate, sEndDate, sCategory, sSiteID, iPartnerID, sPartnerKey)

End Sub

当我运行它时,它不起作用:我得到一个&#34;未经授权的&#34;错误。 我已经仔细检查了数据类型/大小,调用参数,我能想到的所有内容,但我似乎无法弄清楚它为何会在它工作时失败的原因从外部调用C#app。为了确保我没有做一些愚蠢的事情,我创建了第三个应用程序,C#.NET,控制台应用程序。引用了上面创建的相同dll,并且调用有效。这是调用dll的C#控制台应用程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpTest
{
  class Program
  {
      static void Main(string[] args)
      {
        int PartnerID = 329;
        string SiteID = "44265";
        string StartDate = "2017-04-11";
        string EndDate = "2017-04-11";
        string Category = "vacation";
        string Key = "XXXXXXXXX";
        Dictionary<string, decimal> Results = TWPAOI2Lib.TWPLIB.GetAccrualData(StartDate, EndDate, Category, SiteID, PartnerID,Key);
        Console.WriteLine("done");
        Console.ReadLine();
    }
  }
}

有人可以告诉我我做错了什么吗?我无法在C#中继续该程序,因为它不是我的字符串语言; vb.net,这就是我采用这种方法的原因。我对调试/完成这项工作的后续步骤毫无头绪。

感谢所有回复的人! 佛瑞德

1 个答案:

答案 0 :(得分:0)

您在VB和C#示例中以不同顺序传递参数

C#:StartDate,EndDate,Category,SiteID,key,PartnerID

VB:sStartDate,sEndDate,sCategory,sSiteID,iPartnerID,sPartnerKey