LoginSignup
10
15

More than 5 years have passed since last update.

C#でCognitoを利用し、ユーザー登録→認証を実装する。

Last updated at Posted at 2017-09-09

.NetでCognitoを利用した認証をします。
ユーザー登録→ユーザー認証→トークンからユーザー取得の流れで記載します。

前準備

1:ASP.Net MVCでWebアプリケーションを作成

※Microsoft.AspNet.Identity.Coreを利用する前提です。

2:以下をNugetで追加(Verは任意)

• AWSSDK.Core (v3.3.7.1)
• AWSSDK.CognitoIdentity (v 3.3.1.1)
• AWSSDK.CognitoIdentityProvider (v3.3.2.3)

参考ページ
https://aws.amazon.com/jp/blogs/mobile/tag/net/?nc1=f_ls
APIリファレンス
http://docs.aws.amazon.com/ja_jp/cognito-user-identity-pools/latest/APIReference/API_AdminInitiateAuth.html
.Net用リファレンス
http://docs.aws.amazon.com/sdkfornet/v3/apidocs/index.html?page=CognitoIdentityProvider/MCognitoIdentityProviderCognitoIdentityProviderAdminRespondToAuthChallengeAdminRespondToAuthChallengeRequest.html

コード

AWS SDKの作業は、リクエストオブジェクトの仕様を調べ、リクエストを実行。
戻り値の値を確認する事につきるため、慣れると使いやすいです。

User登録

※ClientIdは、ユーザープールにて設定したアプリクライアントのIDを設定します。

                // リクエストオブジェクト生成
                var signUpRequest = new SignUpRequest
                {
                    ClientId = ConfigurationManager.AppSettings["CLIENT_ID"],
                    Password = user.Password,
                    Username = user.Name,
                };

                var emailAttribute = new AttributeType
                {
                    Name = "email",
                    Value = email
                };
                signUpRequest.UserAttributes.Add(emailAttribute);
         // リザルトオブジェクト取得
                var signUpResult = await _client.SignUpAsync(signUpRequest);

User認証

                var authReq = new AdminInitiateAuthRequest()
                {
                    UserPoolId = _poolId,
                    ClientId = _clientId,
                    AuthFlow = AuthFlowType.ADMIN_NO_SRP_AUTH
                };
                authReq.AuthParameters.Add("USERNAME", userName);
                authReq.AuthParameters.Add("PASSWORD", password);

                AdminInitiateAuthResponse authResp = await
_client.AdminInitiateAuthAsync(authReq);

トークンからユーザーを取得


                // AccessTokenを元にUser名を取得
                var getUserReq = new GetUserRequest()
                {
                    AccessToken = authResp.AuthenticationResult.AccessToken
                };

                var getUserResp = _client.GetUser(getUserReq);
                var req = new AdminRespondToAuthChallengeRequest()
                {
                    ChallengeName = ChallengeNameType.ADMIN_NO_SRP_AUTH,
                    ClientId = _clientId,
                    UserPoolId = _poolId,
                    Session = authResp.Session,
                    ChallengeResponses = new Dictionary<string,
string>() { { "USERNAME", userName },{ "PASSWORD", password},{ "SECRET_HASH ",hash } },};
                var resp = _client.AdminRespondToAuthChallenge(req);

セキュアにアクセストークンを管理することだけ意識すれば良くなります。
利用料金も、月間5万まで無料なので、試す分には問題ないです。

10
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
15