[英]VB.Net calling C#.net function with JWT token
To start with, I know very little about C#.NET. 首先,我对C#.NET知之甚少。 What I have comes from a working example that I've modified to get what I need.
我所提供的内容来自一个经过修改的实际示例,可以满足需要。 Trial and error.
反复试验。 What I have is a C#.net console application that works;
我所拥有的是一个工作正常的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)
....
this works up to the point in the code; 这可以达到代码中的要点; I get an authorized token.
我得到了授权令牌。 What I did next was create a new C#.NET project, this time it was a Class Library.
接下来我要做的是创建一个新的C#.NET项目,这次是一个类库。 I compile the Library into a DLL using the same code (copied and pasted).
我使用相同的代码(复制和粘贴)将库编译为DLL。 Then I create a VB.NET console application, reference the C# 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
When I run this, it doesn't work: I get an "Unauthorized" error. 当我运行它时,它不起作用:我收到“未授权”错误。 I've double checked the data types / sizes, calling arguments, everything that I can think of, but I just can't seem to figure out what why it won't work when it's called from outside an C# app.
我已经仔细检查了数据类型/大小,调用参数以及我能想到的所有内容,但是我似乎无法弄清楚为什么从C#应用程序外部调用它时将无法正常工作的原因。 Just to make sure I wasn't doing something stupid, I created a third application, C#.NET, console application.
为了确保我没有做任何愚蠢的事情,我创建了第三个应用程序C#.NET控制台应用程序。 Referenced the same dll created above, and the call works.
引用了与上面创建的相同的dll,该调用有效。 Here's the C# console application that calls the 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();
}
}
}
Can someone please tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗? I can't continue the program in C# because it's not a string language for me;
我无法使用C#继续执行该程序,因为它对我来说不是字符串语言; vb.net is, which is why I'm taking this approach.
vb.net是,这就是为什么我采用这种方法。 I'm clueless as to my next steps in debugging / making this work.
我对调试/使之工作的下一步一无所知。
THanks to all and any who reply! 感谢所有及任何答复! Fred
弗雷德
You are passing the parameters in different order in the VB and C# examples 您正在VB和C#示例中以不同顺序传递参数
C#: StartDate, EndDate, Category, SiteID, key, PartnerID C#:StartDate,EndDate,类别,SiteID,密钥,PartnerID
VB: sStartDate, sEndDate, sCategory, sSiteID, iPartnerID, sPartnerKey VB:sStartDate,sEndDate,sCategory,sSiteID,iPartnerID,sPartnerKey
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.