添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
沉稳的麻辣香锅  ·  NumPy ...·  2 月前    · 

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account Error trying to Add new user with ASP.NET Core Identity: PostgresException: 22021: invalid byte sequence for encoding "UTF8": 0x00 #1069 Error trying to Add new user with ASP.NET Core Identity: PostgresException: 22021: invalid byte sequence for encoding "UTF8": 0x00 #1069 brunobritodev opened this issue Oct 5, 2019 · 13 comments

While trying to add new user, this error happens:

Npgsql.PostgresException (0x80004005): 22021: invalid byte sequence for encoding "UTF8": 0x00
   at Npgsql.NpgsqlConnector.<>c__DisplayClass159_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnector.<>c__DisplayClass159_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
   at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
  Exception data:
    Severity: ERROR
    SqlState: 22021
    MessageText: invalid byte sequence for encoding "UTF8": 0x00
    File: wchar.c
    Line: 2017
    Routine: report_invalid_encoding

Steps to reproduce

I've created a new ASP.NET Core MVC with Identity Enabled. Then deleted default Migration and Created new one with Npgsql.EntityFrameworkCore.PostgreSQL

  • netcoreapp3.0
  • Npgsql.EntityFrameworkCore.PostgreSQL 3.0.1
  •             services.AddDbContext<ApplicationDbContext>(options =>
                    options.UseNpgsql(
                        Configuration.GetConnectionString("DefaultConnection")));
                services.AddDefaultIdentity<IdentityUser>(options =>
                        // Options for simple tests only. Never use this kind of config in prod
                        options.Password.RequireDigit = false;
                        options.Password.RequireLowercase = false;
                        options.Password.RequireNonAlphanumeric = false;
                        options.Password.RequireUppercase = false;
                        options.Password.RequiredLength = 6;
                    .AddDefaultUI()
                    .AddEntityFrameworkStores<ApplicationDbContext>();
    

    Using Postgree from Docker container:

    docker run -p 5432:5432 --name some-postgres -e POSTGRES_PASSWORD=10203040 -d postgres
    

    Connection String: Server=localhost;Port=5432;Database=teste;User Id=postgres;Password=10203040

    I've just created a new ASP.NET webapp from scratch (dotnet new webapp --auth Individual), switched from Sqlite to Npgsql, deleted and recreated the migrations, created the database, and successfully registered a new user.

    The error you're seeing could be the result of trying to copy-paste a malformed password (or email!): PostgreSQL does not support strings which contain 0x00 (ASCII 0), which happens to be the null terminator in C strings. Are you seeing this error with a manually-entered, simple password?

    If so, can someone please link to a runnable, minimal project where this problem occurs, and provide the exact email/password you're trying to register with?

    Regrettably, manually inputting the password doesn't work either. Same happens with any other password.

    Full source of my app can be found here: https://github.com/Atulin/Ogma and the credentials I use is

    name: atulin
    email: [email protected]
    pass: zaq1@WSXcde3

    Edit: Also, full raw error messages

    Npgsql.PostgresException (0x80004005): 22021: niepoprawna sekwencja bajtów dla kodowania "UTF8": 0x00
       at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
       at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
       at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
      Exception data:
        Severity: BŁĄD
        SqlState: 22021
        MessageText: niepoprawna sekwencja bajtów dla kodowania "UTF8": 0x00
        File: d:\pginstaller.auto\postgres.windows-x64\src\backend\utils\mb\wchar.c
        Line: 2017
        Routine: report_invalid_encoding
    
    Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
     ---> Npgsql.PostgresException (0x80004005): 22021: niepoprawna sekwencja bajtów dla kodowania "UTF8": 0x00
       at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
       at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
       at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
      Exception data:
        Severity: BŁĄD
        SqlState: 22021
        MessageText: niepoprawna sekwencja bajtów dla kodowania "UTF8": 0x00
        File: d:\pginstaller.auto\postgres.windows-x64\src\backend\utils\mb\wchar.c
        Line: 2017
        Routine: report_invalid_encoding
       --- End of inner exception stack trace ---
       at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(DbContext _, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
       at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
       at Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9.CreateAsync(TUser user, CancellationToken cancellationToken)
       at Microsoft.AspNetCore.Identity.UserManager`1.CreateAsync(TUser user)
       at Microsoft.AspNetCore.Identity.UserManager`1.CreateAsync(TUser user, String password)
       at Ogma3.Areas.Identity.Pages.Account.RegisterModel.OnPostAsync(String returnUrl) in G:\VS Projects\Ogma3\Ogma3\Areas\Identity\Pages\Account\Register.cshtml.cs:line 93
       at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory.GenericTaskHandlerMethod.Convert[T](Object taskAsObject)
       at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory.GenericTaskHandlerMethod.Execute(Object receiver, Object[] arguments)
       at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeHandlerMethodAsync()
       at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeNextPageFilterAsync()
       at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
       at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
       at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterAsync()
       at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
       at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
       at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
       at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
       at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
       at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
       at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
       at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
       at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
       at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
       at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
       at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
              

    Thanks for the repro, @Atulin. I could reproduce the exception and tracked it down to the password hasher you're using: ScottBrady91.AspNetCore.Identity.Argon2PasswordHasher. That hasher seems to output strings with trailing ASCII-0 characters in them (0x00), which is what trips up PostgreSQL. Commenting out this line makes the error go away.

    I've opened scottbrady91/ScottBrady91.AspNetCore.Identity.Argon2PasswordHasher#2 with Argon2PasswordHasher for them to look into it. In the meantime either use the built-in hasher, or possibly wrap Argon2PasswordHasher with an adapter that strips out the trailing zeros.

    Am closing this issue as I haven't seen other repros, and I suspect they'd also have this as their root cause.

    @roji you are totally right.

    I don't remeber how, but I'm sure that for some reason was working in last version under some circunstances. Using Argon2 as hash.

    When I was upgrading from asp.ne core 2.2 to 3.0, I got stuck with this error, but a lot of modifications was made.

    btw, thanks for your efforts!