簡體   English   中英

在C#無服務器應用程序中使用Cognito

[英]Using Cognito with C# serverless application

我想創建一個使用AWS Cognito進行身份驗證的AWS無服務器應用程序。 我的起點是Visual Studio 2017中C#的AWS Serverless Application with Tests (.NET Core)AWS Serverless Application with Tests (.NET Core)模板。部署后,該模板將創建一些Lambda函數並設置AWS API Gateway,以便我可以通過REST請求連接到Lambda函數。 這可行。

我已經在AWS Cognito中創建了一個用戶池,並創建了一個javascript客戶端(單頁面應用程序),該客戶端可讓用戶使用AWS Cognito登錄。 在javascript客戶端中,我能夠連接到AWS Cognito並以JWT的身份獲取ID,訪問和刷新令牌。 我還可以在Authorization: Bearer eyblablabla...標頭Authorization: Bearer eyblablabla...這些令牌發送到后端(AWS無服務器應用程序)。 因此,javascript客戶端身份驗證和AWS Cognito設置似乎可以正常工作。 我還在AWS API Gateway中設置了一個指向AWS Cognito用戶池的授權者。

我的問題如下:后端似乎不知道授權標頭。 在檢查請求時,我沒有為用戶得到任何索賠。 我對獲得sub權利要求特別感興趣,以便可以識別用戶。 另外,我希望API網關針對特定方法自動發送Unauthorized響應,但是我不知道如何設置。

作為Lambda函數簽名的一部分,我獲得了APIGatewayProxyRequest請求對象。 顯然request.RequestContext.Authorizer.Claims應該包含用戶聲明,但是.Authorizer為null。

我能夠從javascript客戶端y讀取請求標頭獲得發送的所有JWT,因此我可以解析令牌以獲取用戶聲明。 但是我認為我的設置肯定有問題,因為沒有填充.Authorizer

到目前為止,只有我發現的建議涉及serverless.yml或Swagger模板文件中的內容,兩者都不是AWS Serverless Application VS2017模板的一部分。 相反,我有一個serverless.template JSON文件,沒有明顯的方法向該文件添加身份驗證/安全設置。

到目前為止,我的代碼與VS2017中的AWS無服務器應用程序模板相同。

任何幫助將不勝感激。

您需要在API方法集成請求中啟用“使用Lambda代理”集成。 您的APIGatewayProxyRequest對象將如下所示:

{
"Resource": "/test",
"Path": "/test",
"HttpMethod": "GET",
"Headers": {
    "Accept": "*/*",
    "accept-encoding": "gzip, deflate",
    "Authorization": "your token",
    "cache-control": "no-cache",
    "CloudFront-Forwarded-Proto": "https",
    "CloudFront-Is-Desktop-Viewer": "true",
    "CloudFront-Is-Mobile-Viewer": "false",
    "CloudFront-Is-SmartTV-Viewer": "false",
    "CloudFront-Is-Tablet-Viewer": "false",
    "CloudFront-Viewer-Country": "US",
    "Host": "xxxxxxx.execute-api.us-east-1.amazonaws.com",
    "Postman-Token": "08820d50-c5d4-498a-bfee-c76994bb91f1",
    "User-Agent": "PostmanRuntime/7.4.0",
    "Via": "1.1 dd169cfdbbafbb3da513bede6bc6640e.cloudfront.net (CloudFront)",
    "X-Amz-Cf-Id": "89ftx9aaVK0k2KOFu-5QESLXzGUGAw17gNCCY03in-hF2hd-LvRhIg==",
    "X-Amzn-Trace-Id": "Root=1-5c125bb9-1e8b9fea8d1beb20147a24d2",
    "X-Forwarded-For": "50.196.109.21, 70.132.33.133",
    "X-Forwarded-Port": "443",
    "X-Forwarded-Proto": "https"
},
"QueryStringParameters": null,
"PathParameters": null,
"StageVariables": null,
"RequestContext": {
    "Path": "/test_oauth/token",
    "AccountId": "xxxxxxxxxxxxx",
    "ResourceId": "luy67k",
    "Stage": "test_oauth",
    "RequestId": "5455133d-fed9-11e8-8f41-ef35907ced2d",
    "Identity": {
        "CognitoIdentityPoolId": null,
        "AccountId": null,
        "CognitoIdentityId": null,
        "Caller": null,
        "ApiKey": null,
        "SourceIp": "50.196.109.21",
        "CognitoAuthenticationType": null,
        "CognitoAuthenticationProvider": null,
        "UserArn": null,
        "UserAgent": "PostmanRuntime/7.4.0",
        "User": null
    },
    "ResourcePath": "/token",
    "HttpMethod": "GET",
    "ApiId": "8xxg9ez961",
    "Authorizer": {
        "claims": {
            "sub": "4560ac4b-54a0-4184-8831-e3cb2583726b",
            "aud": "xxxxxxxxxxxxxxxx",
            "email_verified": "false",
            "event_id": "467633ad-fed9-11e8-88ff-25be6cd15697",
            "token_use": "id",
            "custom:ApplicationId": "12345",
            "auth_time": "1544706978",
            "iss": "https://cognito-idp.us-east-1.amazonaws.com/us-east-xxxxxxxxx",
            "cognito:username": "username",
            "exp": "Thu Dec 13 14:16:18 UTC 2018",
            "iat": "Thu Dec 13 13:16:18 UTC 2018",
            "email": "user@email.com"
        }
    }
},
"Body": null,
"IsBase64Encoded": false
}

請注意,如果將API網關設置為使用ID令牌和訪問令牌,則會發現不同的值(這是使用自定義OAuth范圍還是不使用API​​網關設置的區別)。 您還會發現結果取決於用戶池應用程序客戶端的設置-啟用了允許的Oauth范圍以及為應用程序客戶端啟用了哪些屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM