- Регистрация
- 23.09.2016
- Сообщения
- 206
- Благодарностей
- 7
- Баллы
- 18
Может кто то сможет подсказать как можно выполнить данный код через зеннопостер для получения токена для api
C#:
Запрос и получение JWT токена:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Net.Http;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using System.Net;
namespace grx_asp
{
class Program
{
static string privateKey = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\priv.txt", Encoding.UTF8);
static string uid = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\uid.txt", Encoding.UTF8);
static async Task Main(string[] args)
{
// Use this line to see the crypto info while debugging:
// Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;
byte[] key = Convert.FromBase64String(privateKey);
RSA rsa = RSA.Create();
rsa.ImportFromPem(Encoding.UTF8.GetChars(key));
var claims = new List<Claim>
{
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Exp, DateTimeOffset.Now.AddMinutes(60).ToUnixTimeSeconds().ToString())
};
SigningCredentials credentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256);
JwtSecurityToken jwtSecurity = new JwtSecurityToken(new JwtHeader(credentials), new JwtPayload(claims));
string jwt = new JwtSecurityTokenHandler().WriteToken(jwtSecurity);
string json = JsonConvert.SerializeObject(new { kid = uid, jwt_token = jwt });
#region sync_using_HttpWebRequest
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(@"https://dauth.stage.garantex.biz/api/v1/sessions/generate_jwt");
byte[] data = Encoding.ASCII.GetBytes(json);
webReq.Method = "POST";
webReq.ContentType = "application/json";
webReq.ContentLength = data.Length;
using (Stream stream = webReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
string respStr;
using (HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse())
{
using (StreamReader sr = new StreamReader(webResp.GetResponseStream()))
{
respStr = sr.ReadToEnd();
}
}
#endregion sync_using_HttpWebRequest
#region async_await_using_HttpClient
var httpClient = new HttpClient();
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage hrm = await httpClient.PostAsync(@"https://dauth.stage.garantex.biz/api/v1/sessions/generate_jwt", content);
HttpStatusCode httpStatus = hrm.StatusCode;
#endregion async_await_using_HttpClient
Console.ReadLine();
}
}
}