I've been trying to integrate AWS Cognito with ASP.net Identity.
I've been following some guidelines, AWS cognito + Identity, but when I decide to use the UserManager to create I get a DBContext Error.
UserManager.CreateAsync(user,registerModel.Password)
The entity type CognitoUser is not part of the model for the current context
Which confuses me since the user database should be on AWS side.
Here are some of the components to my Application.
public class CognitoUser : IdentityUser { public string FirstName { get; set; } public string LastName { get; set; } public string Password { get; set; } public UserStatusType Status { get; set; } public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<CognitoUser> manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here return userIdentity; } } public class CognitoSignInManager : SignInManager<CognitoUser, string> { public CognitoSignInManager(CognitoUserManager userManager, IAuthenticationManager authenticationManager) : base(userManager, authenticationManager) { } public override Task<ClaimsIdentity> CreateUserIdentityAsync(CognitoUser user) { return user.GenerateUserIdentityAsync((CognitoUserManager)UserManager); } public static CognitoSignInManager Create(IdentityFactoryOptions<CognitoSignInManager> options, IOwinContext context) { return new CognitoSignInManager(context.GetUserManager<CognitoUserManager>(), context.Authentication); } } public class CognitoUserManager : UserManager<CognitoUser> { public CognitoUserManager(IUserStore<CognitoUser> store) : base(store) { } public static CognitoUserManager Create(IdentityFactoryOptions<CognitoUserManager> options, IOwinContext context) { var manager = new CognitoUserManager(new UserStore<CognitoUser>()); // Configure validation logic for usernames manager.UserValidator = new UserValidator<CognitoUser>(manager) { AllowOnlyAlphanumericUserNames = true, RequireUniqueEmail = true }; // Configure validation logic for passwords manager.PasswordValidator = new PasswordValidator { RequiredLength = 8, RequireNonLetterOrDigit = true, RequireDigit = true, RequireLowercase = false, RequireUppercase = false, }; // Configure user lockout defaults manager.UserLockoutEnabledByDefault = true; manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); manager.MaxFailedAccessAttemptsBeforeLockout = 5; // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user // You can write your own provider and plug it in here. //manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<CognitoUser> //{ // MessageFormat = "Your security code is {0}" //}); manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<CognitoUser> { Subject = "Security Code", BodyFormat = "Your security code is {0}" }); //manager.EmailService = new EmailService(); //manager.SmsService = new SmsService(); var dataProtectionProvider = options.DataProtectionProvider; if (dataProtectionProvider != null) { manager.UserTokenProvider = new DataProtectorTokenProvider<CognitoUser>(dataProtectionProvider.Create("ASP.NET Identity")); } return manager; } } 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.UserName }; var emailAttribute = new AttributeType { Name = "email", Value = user.Email }; signUpRequest.UserAttributes.Add(emailAttribute); var phoneAttribute = new AttributeType { Name = "phone_number", Value = user.PhoneNumber }; signUpRequest.UserAttributes.Add(phoneAttribute); var firstNameAttribute = new AttributeType { Name = "given_name", Value = user.FirstName }; signUpRequest.UserAttributes.Add(firstNameAttribute); var lastNameattribute = new AttributeType { Name = "family_name", Value = user.LastName }; signUpRequest.UserAttributes.Add(phoneAttribute); var response = _client.SignUpAsync(signUpRequest).Result; return Task.FromResult(user); } //... Some more interface implementations }
So my questions are:
- Why is it throwing that error? Can I make UserManager not use a DB context?
- Am I missing steps?
- Is there anything else I should be implementing?
0 comments:
Post a Comment