간단한 ASP.NET 인증 보일러 플레이트 프로젝트. EF, 데이터베이스 없음, IdentityServer4, 세션 스토리지 없음, 쿠키를위한 기본 로깅 및 몇 가지 권한 부여 정책과 예제가있는 컨트롤러가있는 JWT. 최근 .NET 8로 업데이트되었습니다.
.csproj 파일이있는 루트로 cd 만 올라가고 다음 명령을 실행합니다.
cd examples/cookies+api
dotnet run
이 앱은 기본 그룹 기반 관리자 시스템을 시뮬레이션합니다. 사용자는 users , superusers 및 admins 의 구성원이 될 수있는 세 가지 그룹이 있습니다. 모든 사용자는 users 그룹의 구성원이지만 모두 superusers 및 admins 의 구성원은 아니며 필터 액세스에 대한 정책 기반 액세스와 함께 Authorize 속성을 사용하는 앱의 일부가 있습니다.
테스트 할 일련의 로그인이 있습니다. 홈페이지에는 사용 가능한 사용자 및 비밀번호 테이블이 있으며 사용자가 superusers 및 / 또는 admins 의 구성원인지를 나타냅니다.

그런 다음 아래의 일련의 테스트를 실행하고 로그인에 따라 의존적으로 볼 수있는 섹션을 볼 수 있습니다. 모든 테스트는 ExampleController 클래스를 호출합니다.
http://localhost/auth 로 찾아보고 사용자에 로그인하면 다음이 표시됩니다.
Only authenticated cookie based requests from superusers receive this message.
http://localhost/superuser 로 찾아보고 superusers 또는 admins 의 사용자에 로그인하면 다음을 볼 수 있습니다.
Only authenticated cookie based requests from superusers receive this message.
http://localhost/admin 찾아보고 admins 의 사용자로 로그인하면 다음을 볼 수 있습니다.
Only authenticated cookie based requests from admins receive this message.
첫 번째 명령은 후속 요청에 필요한 토큰을 가져옵니다. 아래의 예에서는 password 있는 사용자 admin 사용되지만 테스트 할 다른 로그인이있어 다른 결과를 제공합니다.
curl -X POST http://localhost:5000/api/login -H "Content-Type: application/x-www-form-urlencoded" -d "Name=admin&Password=password"
위의 명령은 다음과 비슷한 것을 초래해야합니다 (분명히 토큰 값은 다릅니다).
{
"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhZG1pbkBkb21haW4iLCJqdGkiOiJkNGM5MDE0Zi0zOGYxLTQ3NTItODU3YS03ZTc0YzU0MjY3ZDciLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiYWRtaW4iLCJhZG1pbnMiOiIxIiwic3VwZXJ1c2VycyI6IjIiLCJ1c2VycyI6IjMiLCJleHAiOjE1MDQzODk0NTEsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTAwMC9hcGkvIiwiYXVkIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwL2FwaS8ifQ.gFnf39Vj16vEmcI1HdwPajH6sRHusxtjZ2eh0Xc1cJs"
}
그런 다음 토큰 값은 후속 요청과 함께 사용됩니다. 아래 예제의 경우 TOKEN_VALUE 위의 단계에서 얻은 값으로 바꾸십시오.
curl http://localhost:5000/api/auth -H "Authorization: Bearer TOKEN_VALUE"
당신의 토큰이 무엇이든 상관없이 다음을 볼 수 있습니다.
Only authenticated token based requests receive this message.
다음 테스트는 superusers 또는 admins 의 사용자에게 효과가 있습니다.
curl http://localhost:5000/api/superuser -H "Authorization: Bearer TOKEN_VALUE"
위의 명령은 다음을 생성해야합니다.
Only authenticated token based requests from superusers receive this message.
다음 테스트는 admins 의 사용자에게 효과가 있습니다.
curl http://localhost:5000/api/admin -H "Authorization: Bearer TOKEN_VALUE"
위의 명령은 다음을 생성해야합니다.
Only authenticated token based requests from admins receive this message.
다음 예는 JWT 조작을 보여주고, 코드는 ExampleController 클래스의 TokenInfo 메소드에 있습니다.
curl http://localhost:5000/api/tokeninfo -H "Authorization: Bearer TOKEN_VALUE"
인증 된 사용자는 다음과 비슷한 것을 제시해야합니다.
{
"token":{
"actor":null,
"audiences":[
"http://localhost:5000/api/"
],
"claims":[
{
"issuer":"http://localhost:5000/api/",
"originalIssuer":"http://localhost:5000/api/",
"properties":{
},
"subject":null,
"type":"sub",
"value":"admin@domain",
"valueType":"http://www.w3.org/2001/XMLSchema#string"
},
{
"issuer":"http://localhost:5000/api/",
"originalIssuer":"http://localhost:5000/api/",
"properties":{
},
"subject":null,
"type":"jti",
"value":"f85815ae-69c4-4fec-8553-bc1199e3cdce",
"valueType":"http://www.w3.org/2001/XMLSchema#string"
},
{
"issuer":"http://localhost:5000/api/",
"originalIssuer":"http://localhost:5000/api/",
"properties":{
},
"subject":null,
"type":"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
"value":"admin",
"valueType":"http://www.w3.org/2001/XMLSchema#string"
},
{
"issuer":"http://localhost:5000/api/",
"originalIssuer":"http://localhost:5000/api/",
"properties":{
},
"subject":null,
"type":"admins",
"value":"1",
"valueType":"http://www.w3.org/2001/XMLSchema#string"
},
{
"issuer":"http://localhost:5000/api/",
"originalIssuer":"http://localhost:5000/api/",
"properties":{
},
"subject":null,
"type":"superusers",
"value":"2",
"valueType":"http://www.w3.org/2001/XMLSchema#string"
},
{
"issuer":"http://localhost:5000/api/",
"originalIssuer":"http://localhost:5000/api/",
"properties":{
},
"subject":null,
"type":"users",
"value":"3",
"valueType":"http://www.w3.org/2001/XMLSchema#string"
},
{
"issuer":"http://localhost:5000/api/",
"originalIssuer":"http://localhost:5000/api/",
"properties":{
},
"subject":null,
"type":"exp",
"value":"1504392381",
"valueType":"http://www.w3.org/2001/XMLSchema#integer"
},
{
"issuer":"http://localhost:5000/api/",
"originalIssuer":"http://localhost:5000/api/",
"properties":{
},
"subject":null,
"type":"iss",
"value":"http://localhost:5000/api/",
"valueType":"http://www.w3.org/2001/XMLSchema#string"
},
{
"issuer":"http://localhost:5000/api/",
"originalIssuer":"http://localhost:5000/api/",
"properties":{
},
"subject":null,
"type":"aud",
"value":"http://localhost:5000/api/",
"valueType":"http://www.w3.org/2001/XMLSchema#string"
}
],
"encodedHeader":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
"encodedPayload":"eyJzdWIiOiJhZG1pbkBkb21haW4iLCJqdGkiOiJmODU4MTVhZS02OWM0LTRmZWMtODU1My1iYzExOTllM2NkY2UiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiYWRtaW4iLCJhZG1pbnMiOiIxIiwic3VwZXJ1c2VycyI6IjIiLCJ1c2VycyI6IjMiLCJleHAiOjE1MDQzOTIzODEsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTAwMC9hcGkvIiwiYXVkIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwL2FwaS8ifQ",
"header":{
"alg":"HS256",
"typ":"JWT"
},
"id":"f85815ae-69c4-4fec-8553-bc1199e3cdce",
"issuer":"http://localhost:5000/api/",
"payload":{
"sub":"admin@domain",
"jti":"f85815ae-69c4-4fec-8553-bc1199e3cdce",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name":"admin",
"admins":"1",
"superusers":"2",
"users":"3",
"exp":1504392381,
"iss":"http://localhost:5000/api/",
"aud":"http://localhost:5000/api/"
},
"innerToken":null,
"rawAuthenticationTag":null,
"rawCiphertext":null,
"rawData":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhZG1pbkBkb21haW4iLCJqdGkiOiJmODU4MTVhZS02OWM0LTRmZWMtODU1My1iYzExOTllM2NkY2UiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiYWRtaW4iLCJhZG1pbnMiOiIxIiwic3VwZXJ1c2VycyI6IjIiLCJ1c2VycyI6IjMiLCJleHAiOjE1MDQzOTIzODEsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTAwMC9hcGkvIiwiYXVkIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwL2FwaS8ifQ.AXpvgAE3ZFN8EnRVSkLUt0iCaFTySFnMTfSx_kWYFDk",
"rawEncryptedKey":null,
"rawInitializationVector":null,
"rawHeader":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
"rawPayload":"eyJzdWIiOiJhZG1pbkBkb21haW4iLCJqdGkiOiJmODU4MTVhZS02OWM0LTRmZWMtODU1My1iYzExOTllM2NkY2UiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiYWRtaW4iLCJhZG1pbnMiOiIxIiwic3VwZXJ1c2VycyI6IjIiLCJ1c2VycyI6IjMiLCJleHAiOjE1MDQzOTIzODEsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTAwMC9hcGkvIiwiYXVkIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwL2FwaS8ifQ",
"rawSignature":"AXpvgAE3ZFN8EnRVSkLUt0iCaFTySFnMTfSx_kWYFDk",
"securityKey":null,
"signatureAlgorithm":"HS256",
"signingCredentials":null,
"encryptingCredentials":null,
"signingKey":null,
"subject":"admin@domain",
"validFrom":"0001-01-01T00:00:00",
"validTo":"2017-09-02T22:46:21Z"
}
}
주소 http://localhost/auth 쿠키와 JWT 기반 로그인 모두에 사용할 수 있으며 실행 후 다음 메시지를 볼 수 있습니다.
Only authenticated requests receive this message.