AspNet Identity不在IdentityConfig中使用自定义PasswordValidator代码

时间:2015-03-01 23:43:32

标签: asp.net vb.net asp.net-identity

我一直试图解决这个问题超过一个星期,似乎无法解决为什么它不起作用。我正在使用ASP.NET Web Forms。

我在IdentityConfig.vb文件中更新了以下内容以减少密码要求,因此它不需要任何大写或数字值:

 manager.PasswordValidator = New PasswordValidator() With {
          .RequiredLength = 4,
          .RequireNonLetterOrDigit = False,
          .RequireDigit = False,
          .RequireLowercase = False,
          .RequireUppercase = False
        }

这是我创建用户的代码

Protected Sub CreateUser_Click(sender As Object, e As EventArgs)
    Dim userName As String = TextBox1.Text
    Dim manager = Context.GetOwinContext().GetUserManager(Of ApplicationUserManager)()
    Dim signInManager = Context.GetOwinContext().Get(Of ApplicationSignInManager)()
    Dim user = New ApplicationUser() With {.UserName = userName, .Email = userName}
    Dim result = manager.Create(user, TextBox2.Text)
    If result.Succeeded Then
        signInManager.SignIn(user, isPersistent:=False, rememberBrowser:=False)
        Response.Redirect("/member")
    Else
    Literal1.Text = result.Errors.FirstOrDefault()
    End If
End Sub

但出于某种原因,无论我尝试什么,它都使用默认密码要求,并在尝试创建帐户时向我提供以下验证消息。

  

密码必须至少为6个字符。密码必须至少包含一个非字母或数字字符。密码必须至少有一个小写字母('a' - 'z')。密码必须至少有一个大写字母('A' - 'Z')。

我已将注册页面移出/帐户部分。

有人知道我做错了吗?

编辑1:在IdentityConfig文件中引用ApplicationUserManager

Public Class ApplicationUserManager
    Inherits UserManager(Of ApplicationUser)
    Public Sub New(store As IUserStore(Of ApplicationUser))
        MyBase.New(store)
    End Sub

    Public Shared Function Create(options As IdentityFactoryOptions(Of ApplicationUserManager), context As IOwinContext) As ApplicationUserManager
        Dim manager = New ApplicationUserManager(New UserStore(Of ApplicationUser)(context.[Get](Of ApplicationDbContext)()))
        ' Configure validation logic for usernames
        manager.UserValidator = New UserValidator(Of ApplicationUser)(manager) With {
          .AllowOnlyAlphanumericUserNames = False,
          .RequireUniqueEmail = True
        }

        ' Configure validation logic for passwords
        manager.PasswordValidator = New PasswordValidator() With {
          .RequiredLength = 4,
          .RequireNonLetterOrDigit = False,
          .RequireDigit = False,
          .RequireLowercase = False,
          .RequireUppercase = False
        }
        ' Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user. '
        ' You can write your own provider and plug in here. '
        manager.RegisterTwoFactorProvider("Phone Code", New PhoneNumberTokenProvider(Of ApplicationUser)() With {
          .MessageFormat = "Your security code is {0}"
        })
        manager.RegisterTwoFactorProvider("Email Code", New EmailTokenProvider(Of ApplicationUser)() With {
          .Subject = "Security Code",
          .BodyFormat = "Your security code is {0}"
        })

        ' Configure user lockout defaults '
        manager.UserLockoutEnabledByDefault = True
        manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5)
        manager.MaxFailedAccessAttemptsBeforeLockout = 5

        manager.EmailService = New EmailService()
        manager.SmsService = New SmsService()
        Dim dataProtectionProvider = options.DataProtectionProvider
        If dataProtectionProvider IsNot Nothing Then
            manager.UserTokenProvider = New DataProtectorTokenProvider(Of ApplicationUser)(dataProtectionProvider.Create("ASP.NET Identity"))
        End If
        Return manager
    End Function
End Class

0 个答案:

没有答案