老外习惯用email,我们要求的是要手机号。
所以除了采用二次验证(2FA),发现工作流要给用户发送通知短信无法获取对应的手机号。所以对源码进行扩展 增加了liquid获取手机号。
1、下载源码可以clone 也可以下载
2、找到对应modules的user模块
直接在starup.cs 找到 LiquidStartup 添加显示phonenumer
添加一行
nameof(User.PhoneNumber)=> new StringValue(user.PhoneNumber)
整段代码:
o.MemberAccessStrategy.Register<User, FluidValue>((user, name, context) => { return name switch { nameof(User.UserId) => new StringValue(user.UserId), nameof(User.UserName) => new StringValue(user.UserName), nameof(User.NormalizedUserName) => new StringValue(user.NormalizedUserName), nameof(User.Email) => new StringValue(user.Email), nameof(User.NormalizedEmail) => new StringValue(user.NormalizedEmail), nameof(User.EmailConfirmed) => user.EmailConfirmed ? BooleanValue.True : BooleanValue.False, nameof(User.IsEnabled) => user.IsEnabled ? BooleanValue.True : BooleanValue.False, nameof(User.RoleNames) => new ArrayValue(user.RoleNames.Select(x => new StringValue(x)).ToArray()), nameof(User.PhoneNumber)=> new StringValue(user.PhoneNumber), nameof(User.Properties) => new ObjectValue(user.Properties), _ => NilValue.Instance }; });
要是想像email类似的方法调用 就添加一个filter ,位置在 liquid文件夹,
上代码:
`
public class UserPhoneNumberFilter : ILiquidFilter
{
private readonly UserManager
private readonly IHttpContextAccessor _httpContextAccessor;
public UserPhoneNumberFilter(UserManager<IUser> userManager, IHttpContextAccessor httpContextAccessor)
{
_userManager = userManager;
_httpContextAccessor = httpContextAccessor;
}
public async ValueTask<FluidValue> ProcessAsync(FluidValue input, FilterArguments args, LiquidTemplateContext ctx)
{
var value = input.ToObjectValue();
if (value is LiquidUserAccessor)
{
var claimsPrincipal = _httpContextAccessor.HttpContext?.User;
if (claimsPrincipal != null)
{
var phoneNumber = claimsPrincipal.FindFirstValue(ClaimTypes.MobilePhone);
if (phoneNumber != null)
{
return FluidValue.Create(phoneNumber, ctx.Options);
}
}
}
if (value is IUser user)
{
return FluidValue.Create(await _userManager.GetPhoneNumberAsync(user), ctx.Options);
}
return NilValue.Instance;
}
}`
liquid写法:
{% assign user = User | user_id | users_by_id %} {{ user.PhoneNumber}}
效果如下: