Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The HomeController
- ------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Mvc.Ajax;
- using System.Configuration;
- using Microsoft.AspNet.Identity.EntityFramework;
- using Microsoft.AspNet.Identity;
- using Amazon;
- using Amazon.Runtime;
- using Amazon.CognitoIdentityProvider;
- using Amazon.CognitoIdentityProvider.Model;
- using Amazon.CognitoIdentity;
- using Amazon.CognitoSync;
- using System.Threading.Tasks;
- using MasterBuilder.Helpers;
- using MasterBuilder.Models;
- namespace MasterBuilder.Controllers
- {
- //
- //GET /HOME/Index.cshtml
- public class HomeController : Controller
- {
- // /Home/SignUp
- [HttpPost]
- public ActionResult SignUp(string name, string pass, string email)
- {
- CognitoUser user = new CognitoUser()
- {
- UserName = name,
- Password = pass,
- Email = email
- };
- //Create user
- new CognitoUserStore()
- .CreateAsync(user);
- return View("Success");
- }
- [HttpGet]
- public ActionResult SignUp()
- {
- return View();
- }
- //-----------------------------/Home/SignUp
- // /Home/Login
- [HttpGet]
- public ActionResult Index()
- {
- return View();
- }
- [HttpPost]
- public ActionResult Index(string name, string pass)
- {
- //var authReq = new AdminInitiateAuthRequest();
- var cognitoStore = new CognitoUserStore();
- var cognitoManager = new CognitoUserManager(new UserStore<CognitoUser>());
- CognitoUser user = new CognitoUser()
- {
- UserName = name
- };
- if (cognitoManager.CheckPasswordAsync(user, pass).Result)
- ViewBag.Message = "Usuário Valido";
- else
- ViewBag.Message = "Usuário Inválido";
- return View();
- }
- //-----------------------------/Home/Login
- }
- }
- ------
- The Helpers
- ------
- using System;
- using Microsoft.AspNet.Identity.EntityFramework;
- namespace MasterBuilder.Helpers
- {
- public class CognitoUser : IdentityUser
- {
- public string Password { get; set; }
- public Amazon.CognitoIdentityProvider.UserStatusType Status { get; set; }
- }
- }
- ------
- using System;
- using System.Configuration;
- using System.Threading.Tasks;
- using Microsoft.AspNet.Identity;
- using Amazon.CognitoIdentityProvider;
- using Amazon.CognitoIdentityProvider.Model;
- namespace MasterBuilder.Helpers
- {
- public class CognitoUserStore : IUserStore<CognitoUser>,
- IUserLockoutStore<CognitoUser, string>,
- IUserTwoFactorStore<CognitoUser, string>
- {
- private readonly AmazonCognitoIdentityProviderClient _client =
- new AmazonCognitoIdentityProviderClient();
- private readonly string _clientId = ConfigurationManager.AppSettings["CLIENT_ID"];
- private readonly string _poolId = ConfigurationManager.AppSettings["USERPOOL_ID"];
- public Task CreateAsync(CognitoUser user)
- {
- // Register the user using Cognito
- var signUpRequest = new SignUpRequest
- {
- ClientId = ConfigurationManager.AppSettings["CLIENT_ID"],
- Password = user.Password,
- Username = user.Email,
- };
- var emailAttribute = new AttributeType
- {
- Name = "email",
- Value = user.Email
- };
- signUpRequest.UserAttributes.Add(emailAttribute);
- return _client.SignUpAsync(signUpRequest);
- }
- public Task DeleteAsync(CognitoUser user)
- {
- throw new NotImplementedException();
- }
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- public Task<CognitoUser> FindByIdAsync(string userId)
- {
- throw new NotImplementedException();
- }
- public Task<CognitoUser> FindByNameAsync(string userName)
- {
- throw new NotImplementedException();
- }
- public Task<int> GetAccessFailedCountAsync(CognitoUser user)
- {
- throw new NotImplementedException();
- }
- public Task<bool> GetLockoutEnabledAsync(CognitoUser user)
- {
- throw new NotImplementedException();
- }
- public Task<DateTimeOffset> GetLockoutEndDateAsync(CognitoUser user)
- {
- throw new NotImplementedException();
- }
- public Task<bool> GetTwoFactorEnabledAsync(CognitoUser user)
- {
- throw new NotImplementedException();
- }
- public Task<int> IncrementAccessFailedCountAsync(CognitoUser user)
- {
- throw new NotImplementedException();
- }
- public Task ResetAccessFailedCountAsync(CognitoUser user)
- {
- throw new NotImplementedException();
- }
- public Task SetLockoutEnabledAsync(CognitoUser user, bool enabled)
- {
- throw new NotImplementedException();
- }
- public Task SetLockoutEndDateAsync(CognitoUser user, DateTimeOffset lockoutEnd)
- {
- throw new NotImplementedException();
- }
- public Task SetTwoFactorEnabledAsync(CognitoUser user, bool enabled)
- {
- throw new NotImplementedException();
- }
- public Task UpdateAsync(CognitoUser user)
- {
- throw new NotImplementedException();
- }
- }
- }
- ------
- using System.Configuration;
- using Microsoft.AspNet.Identity;
- using Amazon.CognitoIdentityProvider.Model;
- using Amazon.CognitoIdentityProvider;
- using System.Threading.Tasks;
- namespace MasterBuilder.Helpers
- {
- public class CognitoUserManager : UserManager<CognitoUser>
- {
- private readonly AmazonCognitoIdentityProviderClient _client =
- new AmazonCognitoIdentityProviderClient();
- private readonly string _clientId = ConfigurationManager.AppSettings["CLIENT_ID"];
- private readonly string _poolId = ConfigurationManager.AppSettings["USERPOOL_ID"];
- public CognitoUserManager(IUserStore<CognitoUser> store)
- : base(store)
- {
- }
- public override Task<bool> CheckPasswordAsync(CognitoUser user, string password)
- {
- return CheckPasswordAsync(user.UserName, password);
- }
- private async Task<bool> CheckPasswordAsync(string userName, string password)
- {
- try
- {
- var authReq = new AdminInitiateAuthRequest
- {
- UserPoolId = ConfigurationManager.AppSettings["USERPOOL_ID"],
- ClientId = ConfigurationManager.AppSettings["CLIENT_ID"],
- AuthFlow = AuthFlowType.ADMIN_NO_SRP_AUTH
- };
- authReq.AuthParameters.Add("USERNAME", userName);
- authReq.AuthParameters.Add("PASSWORD", password);
- AdminInitiateAuthResponse authResp = await _client.AdminInitiateAuthAsync(authReq);
- return true;
- }
- catch
- {
- return false;
- }
- }
- }
- }
- ------
- ------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement