使用AWS Cognito和ASP.net标识的DBContext错误

时间:2017-12-12 22:47:59

标签: c# amazon-web-services asp.net-identity amazon-cognito

我一直在尝试将AWS Cognito与ASP.net Identity集成。

我一直在遵循一些指导原则,AWS cognito + Identity,但当我决定使用UserManager创建时,我会收到DBContext错误。

UserManager.CreateAsync(user,registerModel.Password)
  

实体类型CognitoUser不是当前模型的一部分   上下文

这使我感到困惑,因为用户数据库应该在AWS端。

以下是我的应用程序的一些组件。

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
}

所以我的问题是:

  • 为什么会抛出这个错误?我可以让UserManager不使用数据库上下文吗?
  • 我错过了台阶吗?
  • 还有什么我应该实施的吗?

0 个答案:

没有答案