gpt4 book ai didi

asp.net - Asp.Net Identity 中带有破折号的身份电子邮件

转载 作者:行者123 更新时间:2023-12-02 08:25:13 26 4
gpt4 key购买 nike

我正在使用 ASP.NET MVC 身份,当我添加 test-name@testing.com 形式的电子邮件时,我收到验证错误消息

User name test-name@testing.com is invalid, can only contain letters or digits.

如何更改验证以便允许电子邮件被接受?

我的cshtml是:

@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { role = "form" }))
{
@Html.AntiForgeryToken()

<div class="reg-header">
<h2>Register</h2>
</div>
<fieldset>
@if (ViewBag.IsRegister)
{
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
}
<div class="margin-bottom-20">
@Html.LabelFor(m => m.RegisterViewModel.FirstName)<br />
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
@Html.TextBoxFor(m => m.RegisterViewModel.FirstName, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(m => m.RegisterViewModel.FirstName, "", new { @class = "text-danger" })
</div>

<div class="margin-bottom-20">
@Html.LabelFor(m => m.RegisterViewModel.LastName)<br />
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
@Html.TextBoxFor(m => m.RegisterViewModel.LastName, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(m => m.RegisterViewModel.LastName, "", new { @class = "text-danger" })
</div>

<div class="margin-bottom-20">
@Html.LabelFor(m => m.RegisterViewModel.Email)<br />
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
@Html.TextBoxFor(m => m.RegisterViewModel.Email, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(m => m.RegisterViewModel.Email, "", new { @class = "text-danger" })
</div>

<div class="margin-bottom-20">
@Html.LabelFor(m => m.RegisterViewModel.Password)<br />
<div class="input-group ">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
@Html.PasswordFor(m => m.RegisterViewModel.Password, new { @class = "form-control" })

</div>
@Html.ValidationMessageFor(m => m.RegisterViewModel.Password, "", new { @class = "text-danger" })
</div>

<div class="margin-bottom-20">
@Html.LabelFor(m => m.RegisterViewModel.ConfirmPassword)<br />
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
@Html.PasswordFor(m => m.RegisterViewModel.ConfirmPassword, new { @class = "form-control" })
<div class="clear"></div>
</div>
@Html.ValidationMessageFor(m => m.RegisterViewModel.ConfirmPassword, "", new { @class = "text-danger" })
</div>
<div class="row">

<div class="col-md-12">
<button class="btn-u" type="submit" name="command" value="Register">Register</button>
</div>
</div>
</fieldset>
}

这是我的 Controller :

                RegisterViewModel model = login.RegisterViewModel;
var user = new ApplicationUser() { UserName = model.Email, FirstName = model.FirstName, LastName = model.LastName, UserRole = UserRole.Rider };
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);

// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

return RedirectToLocal(returnUrl);
//return RedirectToAction("Index", "Home");
}
else
{
ViewBag.ReturnUrl = returnUrl;
AddErrors(result);
}

最佳答案

UserManager 中使用的默认 UserValidator 有一个设置为 true 的 AllowOnlyAlphanumericUserNames 属性。您需要覆盖此属性允许特殊字符。一个可能的解决方案是用您自己的实现覆盖 UserManager,如下所示。

public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store) : base(store)
{
this.UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };
}
}

public class ApplicationUser : IdentityUser
{
//Custom Properties etc.
}

关于asp.net - Asp.Net Identity 中带有破折号的身份电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26726750/

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