Open Closed

Apostrophes in their email address of users Identity Module using faulty [EmailAddress] Attribute #2694


User avatar
0
learnabp created
  • ABP Framework version: v4.3.2
  • UI type: MVC
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace:
  • Steps to reproduce the issue:"

We are working with a client of ours DesertFire Online and we want to allow for users with apostrophes in their email addres for example PeterO'Neil@somedomain.com however the attribute which is used in IdentityUserCreateOrUpdateDtoBase is [EmailAddress] which doesn't accept apostrophes

see below code snnipet

using System;
using System.ComponentModel.DataAnnotations;
using JetBrains.Annotations;
using Volo.Abp.ObjectExtending;
using Volo.Abp.Validation;

namespace Volo.Abp.Identity;

public abstract class IdentityUserCreateOrUpdateDtoBase : ExtensibleObject
{
    [Required]
    [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxUserNameLength))]
    public string UserName { get; set; }

    [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxNameLength))]
    public string Name { get; set; }

    [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxSurnameLength))]
    public string Surname { get; set; }

    [Required]
    **[EmailAddress]**
    [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxEmailLength))]
    public string Email { get; set; }

    [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxPhoneNumberLength))]
    public string PhoneNumber { get; set; }

    public bool IsActive { get; set; }

    public bool LockoutEnabled { get; set; }

    [CanBeNull]
    public string[] RoleNames { get; set; }

    protected IdentityUserCreateOrUpdateDtoBase() : base(false)
    {

    }
}

since this is a abstract class and the properties are not virtual we cannot override this, can you please let us know how we can change the [EmailAddress] attribute to the below regular expression which accepts aprostphes in email addresses please?

[RegularExpression(@"^[\w-']+(\.[\w-']+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$")]


1 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can try to override the application service and use DisableValidationAttribute to disable DTO validation (but you need to verify the DTO manually)

    See: https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Overriding-Services#overriding-a-service-class

    For example:

    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(IIdentityUserAppService), typeof(IdentityUserAppService), typeof(MyIdentityUserAppService))]
    public class MyIdentityUserAppService : IdentityUserAppService
    {
        //.....
        
        [DisableValidation]
        [Authorize(IdentityPermissions.Users.Create)]
        public virtual async Task<IdentityUserDto> CreateAsync(IdentityUserCreateDto input)
        {
            //..validate DTO.....
            return await base.CreateAsync(input);
        }
    }
    
Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11