次のバージョンで確認しました。
$ dotnet --version
6.0.302
次のページで行っているうちのページの途中までを行いました。
Building and Securing Web APIs with ASP.NET Core 3.0
Glossary の項目のひつとを Get するところまでです。
localhost からは次のように取り出しができます。
$ curl --insecure https://localhost:5001/api/glossary/jwt | jq .
{
"term": "JWT",
"definition": "An open, industry standard RFC 7519 method for representing claims securely between two parties. "
}
他のホストからは次のように取り出しができます。
$ curl --insecure https://violet.local:5001/api/glossary/jwt | jq
{
"term": "JWT",
"definition": "An open, industry standard RFC 7519 method for representing claims securely between two parties. "
}
- プロジェクトの作成
dotnet new webapi -o Glossary
2)他のホストからアクセスが出来るように、次のファイルを書き換えます。
appsettings.Development.json
{
"urls": "http://*:5000;https://*:5001",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
appsettings.json
{
"urls": "http://*:5000;https://*:5001",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
3)この時点で実行
dotnet build
dotnet bin/Debug/net6.0/Glossary.dll
4)curl でアクセスして確認
curl --insecure https://localhost:5001/weatherforecast
他の端末からアクセス
curl --insecure https://violet.local:5001/weatherforecast
5)改造するにあたり不要となるファイルの削除
rm WeatherForecast.cs
rm Controllers/WeatherForecastController.cs
- Glossary 関連のファイルの作成
データの項目は次の3つです。
"Access Token", "JWT", "OpenID"
GlossaryItem.cs
//GlossaryItem.cs
namespace Glossary
{
public class GlossaryItem
{
public string? Term { get; set; }
public string? Definition { get; set; }
}
}
Controllers/GlossaryController.cs
//Controllers/GlossaryController.cs
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
namespace Glossary.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class GlossaryController: ControllerBase
{
private static List<GlossaryItem> Glossary = new List<GlossaryItem> {
//leave the glossary untouched
new GlossaryItem
{
Term= "Access Token",
Definition = "A credential that can be used by an application to access an API. It informs the API that the bearer of the token has been authorized to access the API and perform specific actions specified by the scope that has been granted."
},
new GlossaryItem
{
Term= "JWT",
Definition = "An open, industry standard RFC 7519 method for representing claims securely between two parties. "
},
new GlossaryItem
{
Term= "OpenID",
Definition = "An open standard for authentication that allows applications to verify users are who they say they are without needing to collect, store, and therefore become liable for a user’s login information."
}
};
//new code
[HttpGet]
[Route("{term}")]
public ActionResult<GlossaryItem> Get(string term)
{
Console.Error.WriteLine("*** Get *** " + term);
var glossaryItem = Glossary.Find(item =>
item.Term.Equals(term, StringComparison.InvariantCultureIgnoreCase));
if (glossaryItem == null)
{
return NotFound();
} else
{
return Ok(glossaryItem);
}
}
//end new code
}
}
7)実行
dotnet build
dotnet bin/Debug/net6.0/Glossary.dll
8)curl でアクセスして確認
curl --insecure https://localhost:5001/api/glossary/jwt
他の端末からアクセス
curl --insecure https://violet.local:5001/api/glossary/jwt