سؤال

I am using Asp.net Identity 1.0 and wish to use email addresses for 'username'.

After researching I found this post which appears to suggest a solution: AllowOnlyAlphanumericUserNames - how to set it? (RC to RTM breaking change) ASP.NET Identity

I therefore implemented the code (I'm using vb.net):

Public Class AccountController
Inherits Controller

Public Sub New()
    Me.New(New UserManager(Of ApplicationUser)(New UserStore(Of ApplicationUser)(New ApplicationDbContext())))
End Sub

Public Sub New(manager As UserManager(Of ApplicationUser))
    UserManager = manager
    UserManager.UserValidator = New UserValidator(Of ApplicationUser)(UserManager) With {.AllowOnlyAlphanumericUserNames = False}
End Sub

Public Property UserManager As UserManager(Of ApplicationUser)

However when my code calls upon usermanager:

 Dim result = Await UserManager.CreateAsync(user, acct.password)

I get an exception outside the debugger:

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Source Error:

Dim result = Await UserManager.CreateAsync(user, acct.password) Line 294:
If result.Succeeded Then Trace.WriteLine("Succeeded creating: " + acct.username)

Stack Trace:

[DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.]
System.Data.Entity.Internal.InternalContext.SaveChangesAsync(CancellationToken cancellationToken) +219
System.Data.Entity.Internal.LazyInternalContext.SaveChangesAsync(CancellationToken cancellationToken) +66
System.Data.Entity.DbContext.SaveChangesAsync(CancellationToken cancellationToken) +60
System.Data.Entity.DbContext.SaveChangesAsync() +63
Microsoft.AspNet.Identity.EntityFramework.d__0.MoveNext() etc

Seeing as the exception is not caught by the debugger I'm not able to see what 'EntityValidationErrors' is. However, before I inserted my uservalidator I was able to catch the standard 'non-alphanumeric not allowed' exception.

Anyone any thoughts on what I'm doing wrong? Thank you.

هل كانت مفيدة؟

المحلول

Your error happends while entity framework is saving entities to the database. You should read Validation failed for one or more entities. See 'EntityValidationErrors' property for more details

BTW you want to use an email address as a username, so use a correct UserValidator

Public Class EmailUserValidator(Of TUser As IUser)
    Implements IIdentityValidator(Of TUser)

    Public Function ValidateAsync(user As TUser) As Task(Of IdentityResult) Implements IIdentityValidator(Of TUser).ValidateAsync
        Try
            Dim address = New MailAddress(user.UserName)
            Return Task.FromResult(New IdentityResult())
        Catch
            Return Task.FromResult(New IdentityResult("Invalid Email."))
        End Try
    End Function
End Class
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top