簡單的ASP.NET授權樣板項目。沒有EF,沒有數據庫,沒有IdentityServer4,沒有會話存儲,只需使用幾個授權策略和一個帶有一組示例的控制器的Cookie和JWT系統的基本日誌記錄。最近更新為.NET 8。
要啟動並僅運行cd到.csproj文件所在的根並運行以下命令。
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可用於基於cookie和jwt的登錄,用戶應在執行後查看以下消息。
Only authenticated requests receive this message.