locked
Jwt token status code null . Any mistake RRS feed

  • Question

  • User-588203678 posted

    hi

    In C# dotnet core token is not created . Iam using linux server : Kestral . please refer code below

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using JWT.API.Helpers;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using Newtonsoft.Json.Linq;
    using RestSharp;
    
    namespace JWT.API.Controllers
    {
        [Route("api/[controller]")]
        [ApiController]
        
        public class TokenController : ControllerBase
        {
            IUserAuthenticate UsersRepository;
    
            public TokenController(IUserAuthenticate _UsersRepositary)
            {
                UsersRepository = _UsersRepositary;
            }
    
            // POST: api/Token
            [HttpPost]
            [Route("token")]
            public IActionResult token(Users UserModel)
            {
                var client = new RestClient("http://localhost:5125/oauth/token");
                var request = new RestRequest(Method.POST);
                request.AddHeader("content-type", "application/x-www-form-urlencoded");
                //request.AddParameter("application/x-www-form-urlencoded", "grant_type=password&client_id=xamarin&client_secret=secret&username=superardmin", ParameterType.RequestBody);
                request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id=%24%7Baccount.clientId%7D&client_secret=YOUR_CLIENT_SECRET&audience=https%3A%2F%2F%24%7Baccount.namespace%7D%2Fapi%2Fv2%2F", ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);
    
    
                return Ok(response);
            }
        }
    }
    

    It say status  - Not found

       Status  description - Not found

    Any thing i need to install to get JWT token value  ?

    I  want to get Access token value . I have  been struggling here.. need help

    ananD

    Saturday, July 4, 2020 2:30 PM

All replies

  • User1686398519 posted

    Hi guhananth1,

    • According to the "Not found' error message, you can check whether "http://localhost:5125/oauth/token" is correct. You need to make sure that you can access "http://localhost:5125", and ensure this path is correct.
    • About how to get JWT token,you can refer to this link .You need "Microsoft.AspNetCore.Authentication.JwtBearer" and "System.IdentityModel.Tokens.Jwt". For more detailed configuration you need to check the link I gave above.I tested this link and can get the token value very well.

    Below is the code I tested.

            public async Task<IActionResult> IndexAsync()
            {
                AuthenticateResponse result = new AuthenticateResponse();
                string apiUrl = "https://localhost:44399/users/authenticate";
                AuthenticateRequest test = new AuthenticateRequest
                {
                    Username = "test",
                    Password = "test"
                };
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(test);
                var stringContent = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(apiUrl);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = await client.PostAsync(apiUrl, stringContent);
                if (response.IsSuccessStatusCode)
                {
                    var data = await response.Content.ReadAsStringAsync();
                    result = Newtonsoft.Json.JsonConvert.DeserializeObject<AuthenticateResponse>(data);
                }
                return View();
            }

    Here is the result.

    Best Regards,

    YihuiSun

    Monday, July 6, 2020 8:33 AM