'How to consume/generate JWT Token in ASP.net 6 web application MVC without API

I want to implement the Jwt Token in the Asp.net Core 6 web application MVC without using web API. I read so many posts where they are referencing the API only.

And apart from that can we use cookies authentication in our Production environment?



Solution 1:[1]

I want to implement the Jwt Token in the Asp.net Core 6 web application MVC without using web API. I read so many posts where they are referencing the API only.

To consume/generate JWT Token in ASP.net 6 web application MVC, it is similar like in the Web API application, you could create an MVC controller action method, and receive the Login user, then verify the user and call the generate JWT token method to generate the token. After that, you can add the token at the request header and access the protected data.

Refer the following articles and change the API controller to MVC controller:

https://www.c-sharpcorner.com/article/jwt-json-web-token-authentication-in-asp-net-core/

ASP.NET Core JWT Authentication and Authorization of Web API [Detailed]

Apart from that can we use cookies authentication in our Production environment?

Yes, you can use it.

Solution 2:[2]

I would suggest you declare an array outside the function scope and then add changes to it, or you can pass the update array as parameter.

let array = [
  {
    id: "1",
    name: "Slim",
    age: 5,
    location: 1500,
    breed: 500,
  },
  {
    id: "2",
    name: "Sol",
    age: 3,
    location: 1500,
    breed: 1,
  }];

//pass array as a parameter
async getData(array){
    return array
}

//update your array
array.push("your object");

//pass updated array in function call
getData(array);

Solution 3:[3]

You can also use this approach,

async function getData(item) {
  return [
  {
    id: "1",
    name: "Slim",
    age: 5,
    location: 1500,
    breed: 500,
  },
  {
    id: "2",
    name: "Sol",
    age: 3,
    location: 1500,
    breed: 1,
  },
  item
  ]
};

getData(  {
    id: "3",
    name: "obj",
    age: 5,
    location: 1500,
    breed: 1,
  });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 halfer
Solution 2 Suliman Noor
Solution 3 Suliman Noor