Hag*_*rje 25 c# asp.net-identity-2
我想保存没有电子邮件的用户,如下所示:
var user = new ApplicationUser { UserName = model.Name };
var result = await UserManager.CreateAsync(user);
Run Code Online (Sandbox Code Playgroud)
但我得到错误"电子邮件不能为空或空".这有什么解决方案吗?或者这是不可能的?
小智 78
我知道这是旧的,但我不同意接受的答案,因为这个问题被标记为asp.net-identity-2.为了未来读者的利益,ASP.NET Identity 2.0有一个非常简单的解决方案来解决这个问题:
public class ApplicationUserManager : UserManager<ApplicationUser>
{
...snip...
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
// This disables the validation check on email addresses
RequireUniqueEmail = false
};
...snip...
}
}
Run Code Online (Sandbox Code Playgroud)
在UserValidator<TUser>,Task<IdentityResult> ValidateAsync(T item)实现检查此标志并确定它是否应该运行电子邮件验证:
if (this.RequireUniqueEmail)
{
await this.ValidateEmail(item, list);
}
Run Code Online (Sandbox Code Playgroud)
由于您希望在没有电子邮件地址的情况下保存用户,因此您应该这样做.
注意:仅在未收集电子邮件地址时使用此选项.如果您想收集和验证电子邮件地址,但在注册期间将其设为可选,则应使用自定义IIdentityValidator.
小智 30
可以设置ASP Identity 2.2 App_Start\IdentityConfig.cs
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = false
};
Run Code Online (Sandbox Code Playgroud)
身份取决于电子邮件作为重置用户密码的方式.
但是,忽略电子邮件并不简单,但可能.您需要实现Microsoft.AspNet.Identity.IIdentityValidator忽略缺少电子邮件的界面:
namespace Microsoft.AspNet.Identity
{
/// <summary>
/// Used to validate an item
///
/// </summary>
/// <typeparam name="T"/>
public interface IIdentityValidator<in T>
{
/// <summary>
/// Validate the item
///
/// </summary>
/// <param name="item"/>
/// <returns/>
Task<IdentityResult> ValidateAsync(T item);
}
}
Run Code Online (Sandbox Code Playgroud)
然后ApplicationUserManager将您自己的实现分配给属性UserValidator.
如果你真的需要这个,你可以UserValidator通过反编译Microsoft.AspNet.Identity.UserValidator类和窥视现有的源代码并删除电子邮件的cheecking 来获取原始源代码.
但是,我不确定框架的其余部分将如何对用户缺少电子邮件做出反应.可能你会在其他操作中得到例外.
如果有人仍在为此苦苦挣扎......
设置options.User.RequireUniqueEmail=true,创建自定义用户验证器并在注册 Identity 之前注册它。这将使电子邮件成为可选的,但如果输入,仍然要求它是唯一的。
在ConfigureServices(IServiceCollection服务)中:
services.AddTransient<IUserValidator<ApplicationUser>, OptionalEmailUserValidator<MonotypeIdentityUser>>();
services.AddIdentity(...);
Run Code Online (Sandbox Code Playgroud)
自定义用户验证器类:
public class OptionalEmailUserValidator<TUser> : UserValidator<TUser> where TUser : class
{
public OptionalEmailUserValidator(IdentityErrorDescriber errors = null) : base(errors)
{
}
public override async Task<IdentityResult> ValidateAsync(UserManager<TUser> manager, TUser user)
{
var result = await base.ValidateAsync(manager, user);
if(!result.Succeeded && String.IsNullOrWhiteSpace(await manager.GetEmailAsync(user)))
{
var errors = result.Errors.Where(e => e.Code != "InvalidEmail");
result = errors.Count() > 0 ? IdentityResult.Failed(errors.ToArray()) : IdentityResult.Success;
}
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
在启动文件中,您可以通过访问 IdentityOptions 配置禁用 RequireUniqueEmail 此外,您可以在此处更改密码的复杂性,锁定,...
services.Configure<IdentityOptions>(x =>
{
x.User.RequireUniqueEmail = false;
//x.Password.RequireUppercase = false; => other condition for identity
});
Run Code Online (Sandbox Code Playgroud)
你应该使用 Microsoft.AspnetCore.Identity,这里的文档地址
谢谢@PBO,您还可以更改身份选项,如下所示
services.AddDefaultIdentity<IdentityUser>(
options=> {
options.SignIn.RequireConfirmedAccount = false;
options.User.RequireUniqueEmail = false;
options.Password.RequireUppercase = false;
})
.AddEntityFrameworkStores<BookShopDbContext>();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14376 次 |
| 最近记录: |