问题描述
我想保存没有电子邮件的用户,就像这样:
I want to save user without email, like this:
var user = new ApplicationUser { UserName = model.Name };
var result = await UserManager.CreateAsync(user);
但是我收到错误消息电子邮件不能为null或为空".有什么解决办法吗?还是不可能?
But I got error "Email cannot be null or empty". Is there any solution for this? Or it is impossible?
推荐答案
身份取决于电子邮件,作为重置用户密码的一种方式.
Identity depends on email as a way to reset user password.
但是,忽略电子邮件并不简单,但是有可能.您需要实现Microsoft.AspNet.Identity.IIdentityValidator
接口,以忽略缺少电子邮件的情况:
However, ignoring email is not simple, but possible. You'll need to implement Microsoft.AspNet.Identity.IIdentityValidator
interface that ignores the lack of email:
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);
}
}
然后在ApplicationUserManager
中将您自己的实现分配给属性UserValidator
.
And then in ApplicationUserManager
assign your own implementation to property UserValidator
.
如果确实需要此功能,则可以通过反编译Microsoft.AspNet.Identity.UserValidator
类并查看现有源代码并消除电子邮件的麻烦来获取UserValidator
的原始源代码.
If you really need this, you can get the original source code for UserValidator
by decompiling Microsoft.AspNet.Identity.UserValidator
class and peeking into the existing source code and removing cheecking for email.
但是,我不确定该框架的其余部分将如何在缺少用户电子邮件的情况下做出反应.可能您会在其他操作中遇到异常.
However, I'm not sure how the rest of the framework will react on lack of email on user. Probably you'll get exceptions in other operations.
这篇关于Asp.Net Identity无需电子邮件即可保存用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!