gpt4 book ai didi

c# - 你如何编辑一个用户,谁的电子邮件用户名包含一个连字符?

转载 作者:行者123 更新时间:2023-11-30 17:37:29 24 4
gpt4 key购买 nike

我编写了一段代码,用于在 ASP.NET MVC 中查找用户的角色。可以说它适用于大多数用户。但是,如果用户的电子邮件地址中有连字符,则无法更新用户的详细信息。

例如:

我对用户名 test@test.com 进行了多次测试,所有测试都呈阳性。但是,使用相同的用户,但更改他们的用户名 test-test@test.com 或 test@test-test.com 我可以召回用户,但我无法发布任何更改。

Controller 代码:

    [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RoleAddToUser(string UserId, string RoleName)
{
User user = context.Users.Where(u => u.Id.Equals(UserId, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
var userManager = new UserManager<User>(new UserStore<User>(new ApplicationDbContext()));
var idResult = userManager.AddToRole(user.Id, RoleName);
ViewBag.ResultMessage = "Role created successfully !";

return View("ManageUserRoles");
}


---更新---

尝试按照建议将以下代码添加到我的 AccountController 中,但它似乎不起作用。

    public AccountController(UserManager<User> userManager)
{
var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("One");
userManager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<User>(provider.Create("EmailConfirmation"));
UserManager = (ApplicationUserManager)userManager;

UserManager.UserValidator = new UserValidator<User>(UserManager)
{
AllowOnlyAlphanumericUserNames = false
};
}

我的 ApplicationUser 就是用户。


---更新---

通过我在 IdentityConfig.cs 中找到的代码:

        var manager = new ApplicationUserManager(new UserStore<User>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<User>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};

这表明 AllowOnlyAlphanumericUserNames 已设置,并且通过一些测试工作正常。所以这不是问题。该问题似乎只出现在某处嵌入了“-”的电子邮件地址中:

test@test.com - works fine. Users with this email format can update and edit their own email address.

test@test-test.com - does not work.

test-test@test.com - does not work.

可能需要执行更多测试。

欢迎提出任何建议。

最佳答案

我在我的一个项目中重现了这个问题。不确切知道为什么 Identity 不进行更改(如果有空闲时间会尝试进行更改)但是您可以通过在声明时将自定义验证器连接到您的 UserManager 来解决此问题。

manager.UserValidator = new CustomUserValidator<ApplicationUser>(manager);

CustomValidator 应该如下所示:

public class CustomUserValidator<TUser> : IIdentityValidator<TUser>
where TUser : class, Microsoft.AspNet.Identity.IUser
{
private static readonly Regex EmailRegex = new Regex(Consts.EmailRegex, RegexOptions.Compiled | RegexOptions.IgnoreCase);
//in my case EmailRegex = @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"; but you can use your own
private readonly UserManager<TUser> _manager;

public CustomUserValidator()
{
}

public CustomUserValidator(UserManager<TUser> manager)
{
_manager = manager;
}

public async Task<IdentityResult> ValidateAsync(TUser item)
{
var errors = new List<string>();
if (!EmailRegex.IsMatch(item.UserName))
errors.Add("Bad email address");

if (_manager != null)
{
var otherAccount = await _manager.FindByNameAsync(item.UserName);
if (otherAccount != null && otherAccount.Id != item.Id)
errors.Add("Email already exists");
}

return errors.Any()
? IdentityResult.Failed(errors.ToArray())
: IdentityResult.Success;
}
}

ValidateAsync 将在验证新用户时使用(如果对现有用户进行任何更改,则很明显),您可以应用自己的电子邮件正则表达式和其他验证

关于c# - 你如何编辑一个用户,谁的电子邮件用户名包含一个连字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37965365/

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