Open Closed

Error trying to test register method using Identity #6197


User avatar
0
hayash created

hi team

Message Error: Volo.Abp.AbpException: 'Options must be derived from the Volo.Abp.Options.AbpDynamicOptionsManager`1!' in this function "await IdentityOptions.SetAsync(); "when i test register any idea ?


7 Answer(s)
  • User Avatar
    0
    Anjali_Musmade created
    Support Team Support Team Member

    Hello,

    Can you please provide detailed steps to reproduce the issue?

  • User Avatar
    0
    hayash created

    here is registration function

    public virtual async Task<IdentityUserDto> RegisterAsync(RegisterDto input) {

        await CheckSelfRegistrationAsync();
    
        await IdentityOptions.SetAsync();
    
        var user = new IdentityUser(GuidGenerator.Create(), input.UserName, input.EmailAddress, CurrentTenant.Id);
    
        input.MapExtraPropertiesTo(user);
    
        (await UserManager.CreateAsync(user, input.Password)).CheckErrors();
    
        await UserManager.SetEmailAsync(user, input.EmailAddress);
        await UserManager.AddDefaultRolesAsync(user);
    
        return ObjectMapper.Map&lt;IdentityUser, IdentityUserDto&gt;(user);
    }
    and i use using Microsoft.Extensions.Options; , this function throw exception in await IdentityOptions.SetAsync();
    

    Message Error: Volo.Abp.AbpException: 'Options must be derived from the Volo.Abp.Options.AbpDynamicOptionsManager`1!' in this function "await

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Please share the full code of registration function. Thanks

  • User Avatar
    0
    hayash created
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Identity;
    using Microsoft.Extensions.Options;
    using Volo.Abp.Account.Emailing;
    using Volo.Abp.Account.Localization;
    using Volo.Abp.Account.Settings;
    using Volo.Abp.Application.Services;
    using Volo.Abp.Auditing;
    using Volo.Abp.DependencyInjection;
    using Volo.Abp.Identity;
    using Volo.Abp.ObjectExtending;
    using Volo.Abp.Settings;
    using IdentityUser = Volo.Abp.Identity.IdentityUser;
    namespace Volo.Abp.Account;
    [Audited]
    public class AccountAppService : ApplicationService, IAccountAppService, ITransientDependency
    {
        protected IIdentityRoleRepository RoleRepository { get; }
        protected IdentityUserManager UserManager { get; }
        protected IAccountEmailer AccountEmailer { get; }
        protected IdentitySecurityLogManager IdentitySecurityLogManager { get; }
        protected IOptions<IdentityOptions> IdentityOptions { get; }
        public AccountAppService(
            IdentityUserManager userManager,
            IIdentityRoleRepository roleRepository,
            IAccountEmailer accountEmailer,
            IdentitySecurityLogManager identitySecurityLogManager,
            IOptions<IdentityOptions> identityOptions)
        {
            RoleRepository = roleRepository;
            AccountEmailer = accountEmailer;
            IdentitySecurityLogManager = identitySecurityLogManager;
            UserManager = userManager;
            IdentityOptions = identityOptions;
            LocalizationResource = typeof(AccountResource);
        }
        public virtual async Task<IdentityUserDto> RegisterAsync(RegisterDto input)
        {
            await CheckSelfRegistrationAsync();
            await IdentityOptions.SetAsync();
            var user = new IdentityUser(GuidGenerator.Create(), input.UserName, input.EmailAddress, CurrentTenant.Id);
            input.MapExtraPropertiesTo(user);
            (await UserManager.CreateAsync(user, input.Password)).CheckErrors();
            await UserManager.SetEmailAsync(user, input.EmailAddress);
            await UserManager.AddDefaultRolesAsync(user);
            return ObjectMapper.Map<IdentityUser, IdentityUserDto>(user);
        }
        public virtual async Task SendPasswordResetCodeAsync(SendPasswordResetCodeDto input)
        {
            var user = await GetUserByEmailAsync(input.Email);
            var resetToken = await UserManager.GeneratePasswordResetTokenAsync(user);
            await AccountEmailer.SendPasswordResetLinkAsync(user, resetToken, input.AppName, input.ReturnUrl, input.ReturnUrlHash);
        }
        public virtual async Task ResetPasswordAsync(ResetPasswordDto input)
        {
            await IdentityOptions.SetAsync();
            var user = await UserManager.GetByIdAsync(input.UserId);
            (await UserManager.ResetPasswordAsync(user, input.ResetToken, input.Password)).CheckErrors();
            await IdentitySecurityLogManager.SaveAsync(new IdentitySecurityLogContext
            {
                Identity = IdentitySecurityLogIdentityConsts.Identity,
                Action = IdentitySecurityLogActionConsts.ChangePassword
            });
        }
        protected virtual async Task<IdentityUser> GetUserByEmailAsync(string email)
        {
            var user = await UserManager.FindByEmailAsync(email);
            if (user == null)
            {
                throw new UserFriendlyException(L["Volo.Account:InvalidEmailAddress", email]);
            }
            return user;
        }
        protected virtual async Task CheckSelfRegistrationAsync()
        {
            if (!await SettingProvider.IsTrueAsync(AccountSettingNames.IsSelfRegistrationEnabled))
            {
                throw new UserFriendlyException(L["SelfRegistrationDisabledMessage"]);
            }
        }
    }
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Is your app depend on the AbpIdentityDomainModule?

    Because we need this line of code.

    context.Services.AddAbpDynamicOptions<IdentityOptions, AbpIdentityOptionsManager>();

  • User Avatar
    0
    hayash created

    ok, i user Volo.Abp.Identity.Domain as a project reference to Volo.Abp.Account.HttpApi and in AbpIdentityDomainModule here the code

    public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddAutoMapperObjectMapper<AbpIdentityDomainModule>();

        Configure&lt;AbpAutoMapperOptions&gt;(options =>
        {
            options.AddProfile&lt;IdentityDomainMappingProfile&gt;(validate: true);
        });
    
        Configure&lt;AbpDistributedEntityEventOptions&gt;(options =>
        {
            options.EtoMappings.Add&lt;IdentityUser, UserEto&gt;(typeof(AbpIdentityDomainModule));
            options.EtoMappings.Add&lt;IdentityClaimType, IdentityClaimTypeEto&gt;(typeof(AbpIdentityDomainModule));
            options.EtoMappings.Add&lt;IdentityRole, IdentityRoleEto&gt;(typeof(AbpIdentityDomainModule));
            options.EtoMappings.Add&lt;OrganizationUnit, OrganizationUnitEto&gt;(typeof(AbpIdentityDomainModule));
    
            options.AutoEventSelectors.Add&lt;IdentityUser&gt;();
            options.AutoEventSelectors.Add&lt;IdentityRole&gt;();
        });
    
        var identityBuilder = context.Services.AddAbpIdentity(options =>
        {
            options.User.RequireUniqueEmail = true;
        });
    
        context.Services.AddObjectAccessor(identityBuilder);
        context.Services.ExecutePreConfiguredActions(identityBuilder);
    
        Configure&lt;IdentityOptions&gt;(options =>
        {
            options.ClaimsIdentity.UserIdClaimType = AbpClaimTypes.UserId;
            options.ClaimsIdentity.UserNameClaimType = AbpClaimTypes.UserName;
            options.ClaimsIdentity.RoleClaimType = AbpClaimTypes.Role;
            options.ClaimsIdentity.EmailClaimType = AbpClaimTypes.Email;
        });
    
        context.Services.AddAbpDynamicOptions&lt;IdentityOptions, AbpIdentityOptionsManager&gt;();
    }
    

    that's what you mean ???

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you share a simple project to reproduce?

    liming.ma@volosoft.com

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11