简体   繁体   English

ASP.NET身份问题

[英]Asp.NET identity issue

I am using asp.Identity to do the users and roles module in my application. 我正在使用asp.Identity在应用程序中执行用户和角色模块。 I create user like this 我这样创建用户

var user = new ApplicationUser() { UserName = name, Email = email };
IdentityResult result1 = ApplicationUserManager.AppUserManager.Create(user, password);

It creates the user, the issue is that in the Application Manager it doesn't check for duplicate email. 它创建了用户,问题是在应用程序管理器中它不检查重复的电子邮件。 My application manager looks like this 我的应用程序经理看起来像这样

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    {
        var manager = new ApplicationUserManager(new EntityUserStore<ApplicationUser, Account, ApplicationRole, Role>());

        AppUserManager = manager;

        // Configure validation logic for usernames
        manager.UserValidator = new UserValidator<ApplicationUser>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };
        // Configure validation logic for passwords
        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = false,
            RequireDigit = false,
            RequireLowercase = false,
            RequireUppercase = false,
        };
        var dataProtectionProvider = options.DataProtectionProvider;
        if (dataProtectionProvider != null)
        {
            manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
        }
        return manager;
}

The other is issue that, if I login using user name it works but if I use emal it returns null. 另一个问题是,如果我使用用户名登录,则可以,但是如果使用emal,则返回null。

 ApplicationUser user = UserManager.FindByEmail(email); // this returns null

Anyone familiar with this issue? 有人熟悉这个问题吗?

Your ApplicationUserManager.AppUserManager.Create does not validate the email because you are not referring to the ApplicationUserManager context,which is something like this: 您的ApplicationUserManager.AppUserManager.Create无法验证电子邮件,因为您未引用ApplicationUserManager上下文,如下所示:

var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>(); 
var user = new ApplicationUser() { UserName = name, Email = email }; 
IdentityResult result = manager.Create(user, password); 
if (result.Succeeded)

The above example var manager will contain the ApplicationUserManager context and validation of email will be done by RequireUniqueEmail = true . 上面的示例var manager将包含ApplicationUserManager上下文,电子邮件的验证将通过RequireUniqueEmail = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM