Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.44 KB | None | 0 0
  1. The HomeController
  2.  
  3. ------
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using System.Web.Mvc.Ajax;
  10. using System.Configuration;
  11. using Microsoft.AspNet.Identity.EntityFramework;
  12. using Microsoft.AspNet.Identity;
  13. using Amazon;
  14. using Amazon.Runtime;
  15. using Amazon.CognitoIdentityProvider;
  16. using Amazon.CognitoIdentityProvider.Model;
  17. using Amazon.CognitoIdentity;
  18. using Amazon.CognitoSync;
  19. using System.Threading.Tasks;
  20. using MasterBuilder.Helpers;
  21. using MasterBuilder.Models;
  22.  
  23. namespace MasterBuilder.Controllers
  24. {
  25.     //
  26.     //GET /HOME/Index.cshtml
  27.         public class HomeController : Controller
  28.     {
  29.         // /Home/SignUp
  30.         [HttpPost]
  31.         public ActionResult SignUp(string name, string pass, string email)
  32.         {
  33.  
  34.             CognitoUser user = new CognitoUser()
  35.             {
  36.                 UserName = name,
  37.                 Password = pass,
  38.                 Email = email
  39.             };
  40.  
  41.             //Create user
  42.             new CognitoUserStore()
  43.              .CreateAsync(user);
  44.             return View("Success");
  45.         }
  46.  
  47.         [HttpGet]
  48.         public ActionResult SignUp()
  49.         {
  50.            
  51.             return View();
  52.         }
  53.         //-----------------------------/Home/SignUp
  54.  
  55.         // /Home/Login
  56.         [HttpGet]
  57.         public ActionResult Index()
  58.         {
  59.  
  60.             return View();
  61.         }
  62.  
  63.         [HttpPost]
  64.         public ActionResult Index(string name, string pass)
  65.         {
  66.  
  67.             //var authReq = new AdminInitiateAuthRequest();
  68.                
  69.                
  70.             var cognitoStore = new CognitoUserStore();
  71.  
  72.             var cognitoManager = new CognitoUserManager(new UserStore<CognitoUser>());
  73.                        
  74.             CognitoUser user = new CognitoUser()
  75.             {
  76.                 UserName = name
  77.             };
  78.  
  79.             if (cognitoManager.CheckPasswordAsync(user, pass).Result)
  80.                 ViewBag.Message = "Usuário Valido";
  81.  
  82.             else
  83.                 ViewBag.Message = "Usuário Inválido";
  84.            
  85.             return View();
  86.  
  87.         }
  88.         //-----------------------------/Home/Login
  89.  
  90.  
  91.     }
  92. }
  93. ------
  94.  
  95. The Helpers
  96.  
  97. ------
  98. using System;
  99. using Microsoft.AspNet.Identity.EntityFramework;
  100.  
  101. namespace MasterBuilder.Helpers
  102. {
  103.     public class CognitoUser : IdentityUser
  104.     {
  105.         public string Password { get; set; }
  106.         public Amazon.CognitoIdentityProvider.UserStatusType Status { get; set; }
  107.     }
  108. }
  109. ------
  110. using System;
  111. using System.Configuration;
  112. using System.Threading.Tasks;
  113. using Microsoft.AspNet.Identity;
  114. using Amazon.CognitoIdentityProvider;
  115. using Amazon.CognitoIdentityProvider.Model;
  116.  
  117. namespace MasterBuilder.Helpers
  118. {
  119.     public class CognitoUserStore : IUserStore<CognitoUser>,
  120.                                   IUserLockoutStore<CognitoUser, string>,
  121.                                   IUserTwoFactorStore<CognitoUser, string>
  122.     {
  123.         private readonly AmazonCognitoIdentityProviderClient _client =
  124.             new AmazonCognitoIdentityProviderClient();
  125.         private readonly string _clientId = ConfigurationManager.AppSettings["CLIENT_ID"];
  126.         private readonly string _poolId = ConfigurationManager.AppSettings["USERPOOL_ID"];
  127.  
  128.         public Task CreateAsync(CognitoUser user)
  129.         {
  130.             // Register the user using Cognito
  131.             var signUpRequest = new SignUpRequest
  132.             {
  133.                 ClientId = ConfigurationManager.AppSettings["CLIENT_ID"],
  134.                 Password = user.Password,
  135.                 Username = user.Email,
  136.  
  137.             };
  138.  
  139.             var emailAttribute = new AttributeType
  140.             {
  141.                 Name = "email",
  142.                 Value = user.Email
  143.             };
  144.             signUpRequest.UserAttributes.Add(emailAttribute);
  145.  
  146.             return _client.SignUpAsync(signUpRequest);
  147.         }
  148.  
  149.         public Task DeleteAsync(CognitoUser user)
  150.         {
  151.             throw new NotImplementedException();
  152.         }
  153.  
  154.         public void Dispose()
  155.         {
  156.             throw new NotImplementedException();
  157.         }
  158.  
  159.         public Task<CognitoUser> FindByIdAsync(string userId)
  160.         {
  161.             throw new NotImplementedException();
  162.         }
  163.  
  164.         public Task<CognitoUser> FindByNameAsync(string userName)
  165.         {
  166.             throw new NotImplementedException();
  167.         }
  168.  
  169.         public Task<int> GetAccessFailedCountAsync(CognitoUser user)
  170.         {
  171.             throw new NotImplementedException();
  172.         }
  173.  
  174.         public Task<bool> GetLockoutEnabledAsync(CognitoUser user)
  175.         {
  176.             throw new NotImplementedException();
  177.         }
  178.  
  179.         public Task<DateTimeOffset> GetLockoutEndDateAsync(CognitoUser user)
  180.         {
  181.             throw new NotImplementedException();
  182.         }
  183.  
  184.         public Task<bool> GetTwoFactorEnabledAsync(CognitoUser user)
  185.         {
  186.             throw new NotImplementedException();
  187.         }
  188.  
  189.         public Task<int> IncrementAccessFailedCountAsync(CognitoUser user)
  190.         {
  191.             throw new NotImplementedException();
  192.         }
  193.  
  194.         public Task ResetAccessFailedCountAsync(CognitoUser user)
  195.         {
  196.             throw new NotImplementedException();
  197.         }
  198.  
  199.         public Task SetLockoutEnabledAsync(CognitoUser user, bool enabled)
  200.         {
  201.             throw new NotImplementedException();
  202.         }
  203.  
  204.         public Task SetLockoutEndDateAsync(CognitoUser user, DateTimeOffset lockoutEnd)
  205.         {
  206.             throw new NotImplementedException();
  207.         }
  208.  
  209.         public Task SetTwoFactorEnabledAsync(CognitoUser user, bool enabled)
  210.         {
  211.             throw new NotImplementedException();
  212.         }
  213.  
  214.         public Task UpdateAsync(CognitoUser user)
  215.         {
  216.             throw new NotImplementedException();
  217.         }
  218.     }
  219. }
  220. ------
  221. using System.Configuration;
  222. using Microsoft.AspNet.Identity;
  223. using Amazon.CognitoIdentityProvider.Model;
  224. using Amazon.CognitoIdentityProvider;
  225. using System.Threading.Tasks;
  226.  
  227. namespace MasterBuilder.Helpers
  228. {
  229.     public class CognitoUserManager : UserManager<CognitoUser>
  230.     {
  231.  
  232.         private readonly AmazonCognitoIdentityProviderClient _client =
  233.             new AmazonCognitoIdentityProviderClient();
  234.         private readonly string _clientId = ConfigurationManager.AppSettings["CLIENT_ID"];
  235.         private readonly string _poolId = ConfigurationManager.AppSettings["USERPOOL_ID"];
  236.  
  237.         public CognitoUserManager(IUserStore<CognitoUser> store)
  238.             : base(store)
  239.         {
  240.         }
  241.  
  242.         public override Task<bool> CheckPasswordAsync(CognitoUser user, string password)
  243.         {
  244.             return CheckPasswordAsync(user.UserName, password);
  245.         }
  246.  
  247.         private async Task<bool> CheckPasswordAsync(string userName, string password)
  248.         {
  249.             try
  250.             {
  251.                 var authReq = new AdminInitiateAuthRequest
  252.                 {
  253.                     UserPoolId = ConfigurationManager.AppSettings["USERPOOL_ID"],
  254.                     ClientId = ConfigurationManager.AppSettings["CLIENT_ID"],
  255.                     AuthFlow = AuthFlowType.ADMIN_NO_SRP_AUTH
  256.                 };
  257.                 authReq.AuthParameters.Add("USERNAME", userName);
  258.                 authReq.AuthParameters.Add("PASSWORD", password);
  259.  
  260.                 AdminInitiateAuthResponse authResp = await _client.AdminInitiateAuthAsync(authReq);
  261.  
  262.                 return true;
  263.             }
  264.             catch
  265.             {
  266.                 return false;
  267.             }
  268.         }
  269.     }
  270. }
  271.  
  272. ------
  273.  
  274. ------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement