简体   繁体   English

将node.js Jwt示例转换为.Net

[英]Converting node.js Jwt example to .Net

I am trying to use a web api. 我正在尝试使用网络API。 They require that I use Jwt tokens. 他们要求我使用Jwt令牌。 I am trying to do this in a asp.net 4.5.2 web app on azure. 我试图在azure上的asp.net 4.5.2 Web应用程序中执行此操作。 I have downloaded the Microsoft.IdentityModel.Tokens Nuget package and would like to use it to generate the needed tokens. 我已经下载了Microsoft.IdentityModel.Tokens Nuget包,并希望使用它来生成所需的令牌。

The web api I am trying to use has an example in node.js on how to generate the proper token. 我尝试使用的Web API在node.js中有一个有关如何生成正确令牌的示例。 Here is their example: 这是他们的例子:

var jwt = require('jsonwebtoken');

var payload = {
    iss: api_key,
    exp: ((new Date()).getTime() + 5000)
};

//Automatically creates header, and returns JWT
var token = jwt.sign(payload, api_secret);

All of the examples for the nuget package do a ton more than what is shown in the node.js example. nuget包的所有示例都比node.js示例中显示的功能多得多。 Things like creating a secret value, claims identity, signing credentials and all kinds of other stuff. 诸如创建秘密价值,声明身份,签署凭证以及其他各种东西之类的事情。

I have the api_key value and the api_secret value. 我有api_key值和api_secret值。 Can someone please tell me how to do the same thing using the nuget package as what they show using node.js? 有人可以告诉我如何使用nuget软件包执行与使用node.js进行显示相同的操作吗?

After doing some research I think I have a solution. 经过一些研究,我想我有一个解决方案。 This is the simplest it gets in c# unfortunately, but I think its a passable solution considering we are talking apples and oranges. 不幸的是,这是C#中最简单的方法,但是考虑到我们在谈论苹果和橙子,我认为这是一个可行的解决方案。 It seems C# is twice as verbose about the options and doesn't have sensible defaults for anything. 似乎C#在选项上的详细程度是其两倍,并且没有任何合理的默认值。

using System;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Tokens;


namespace stackoverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            String api_key = "apiKey123123";
            String api_secret = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";

            var signingKey = Convert.FromBase64String(api_secret);

            JwtHeader jwtHeader = new JwtHeader(
               new SigningCredentials(
                   new SymmetricSecurityKey(signingKey),
                   SecurityAlgorithms.HmacSha256Signature
               )
            );

            JwtPayload jwtPayload = new JwtPayload {
                {"iss", api_key},
                {"exp", ((DateTimeOffset)DateTime.UtcNow).AddMilliseconds(5000).ToUnixTimeMilliseconds() }
            };

            var jwt = new JwtSecurityToken(jwtHeader, jwtPayload);
            var jwtHandler = new JwtSecurityTokenHandler();
            Console.Write(jwtHandler.WriteToken(jwt));

            Console.ReadLine();
        }
    }
}

Only import was this package: 此包仅导入:

<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.2.1" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM