gpt4 book ai didi

asp.net - 如何自定义 Asp.net Identity 2 用户名已获取验证消息?

转载 作者:行者123 更新时间:2023-12-03 11:42:13 24 4
gpt4 key购买 nike

如何自定义 Asp.net Identity 2 用户名已被采用的验证消息(名称 XYZ 已被采用。)?谢谢

最佳答案

好吧,我没有找到解决此问题的任何简单方法。简单地说,我的意思是修改属性/模型/ Controller 中的一些消息。

一种可能的解决方案是:

执行后

var result = await UserManager.CreateAsync(user, model.Password);

如果结果不成功,您可以检查它的“名称 XYZ 已被占用”的 Errors 属性。模式并将其替换为您的自定义消息。

另一种解决方案(这是我的首选方式)是编写自定义 UserValidation类(class):
 /// <summary>
/// Validates users before they are saved to an IUserStore
/// </summary>
/// <typeparam name="TUser"></typeparam>
public class CustomUserValidator<TUser> : UserValidator<TUser, string>
where TUser : ApplicationUser
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="manager"></param>
public CustomUserValidator(UserManager<TUser, string> manager) : base(manager)
{
this.Manager = manager;
}

private UserManager<TUser, string> Manager { get; set; }

/// <summary>
/// Validates a user before saving
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public override async Task<IdentityResult> ValidateAsync(TUser item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
var errors = new List<string>();
await ValidateUserName(item, errors);
if (RequireUniqueEmail)
{
await ValidateEmail(item, errors);
}
if (errors.Count > 0)
{
return IdentityResult.Failed(errors.ToArray());
}
return IdentityResult.Success;
}

private async Task ValidateUserName(TUser user, List<string> errors)
{
if (string.IsNullOrWhiteSpace(user.UserName))
{
errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.PropertyTooShort, "Name"));
}
else if (AllowOnlyAlphanumericUserNames && !Regex.IsMatch(user.UserName, @"^[A-Za-z0-9@_\.]+$"))
{
// If any characters are not letters or digits, its an illegal user name
errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.InvalidUserName, user.UserName));
}
else
{
var owner = await Manager.FindByNameAsync(user.UserName);
if (owner != null && !EqualityComparer<string>.Default.Equals(owner.Id, user.Id))
{
errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.DuplicateName, user.UserName));
}
}
}

// make sure email is not empty, valid, and unique
private async Task ValidateEmail(TUser user, List<string> errors)
{
if (!user.Email.IsNullOrWhiteSpace())
{
if (string.IsNullOrWhiteSpace(user.Email))
{
errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.PropertyTooShort, "Email"));
return;
}
try
{
var m = new MailAddress(user.Email);
}
catch (FormatException)
{
errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.InvalidEmail, email));
return;
}
}
var owner = await Manager.FindByEmailAsync(user.Email);
if (owner != null && !EqualityComparer<string>.Default.Equals(owner.Id, user.Id))
{
errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.DuplicateEmail, email));
}
}
}

您可以看到正在使用的所有验证错误消息资源,因此通过在资源中指定自定义格式,您将能够自定义这些消息。

您可以在 ApplicationUserManager 中注册您的验证器类(class), Create方法:
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
{
manager.UserValidator = new CustomUserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
}

关于asp.net - 如何自定义 Asp.net Identity 2 用户名已获取验证消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27655578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com