Base solution for your next web application
Open Closed

Insert User and OrganizationUnits without using AbpControllers #9151


User avatar
0
SASIMEXICO created

I am making an asp.net core 3.1 service for inserting data directly to the DB without having to make https calls to the asp.net Zero AppService. The DB where we have the solution is PostgreSQL. My question is the following, how can I do it, if possible, how can I register a user without going through the Abp controllers...but I have managed to insert them in DB, but the users that I register directly in the AbpUsers table do not give internal error when I want to access them from the web.

Regards


8 Answer(s)
  • User Avatar
    0
    musa.demir created

    Your question is not explanatory enough.

    What do you need to implement in your project? What is the situation that prevents what you are trying to add?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @SASIMEXICO,

    If your project which you insert users is an AspNet Boilerplate project, you can use UserManager class to insert users. If not, you can still insert records to database like you do. Could you share what is the error message you are getting ? Probalby some fileds have wrong values.

    You can take a look at these;

    • Be sure that IsActive field is true
    • Be sure that NoramlizedUSername and NormalizedEmailAddress fields contains UPPERCASE values of related fields.
    • Be sure that IsEmailConfirmed field is true.
  • User Avatar
    0
    SASIMEXICO created

    Hi ismcagdas,

    Sorry for my English. I don't use an AspNet Boilerplate project, I'm insert the record directly in postgresql database

    INSERT INTO "AbpUsers" ("CreationTime", "CreatorUserId", "LastModificationTime", "LastModifierUserId", "IsDeleted", "DeleterUserId", "DeletionTime", "AccessFailedCount", "IsPhoneNumberConfirmed", "IsEmailConfirmed", "NormalizedUserName", "NormalizedEmailAddress", "UserName", "TenantId", "EmailAddress", "Name", "Surname", "Password", "PhoneNumber", "IsActive", "ShouldChangePasswordOnNextLogin", "IsTwoFactorEnabled", "IsLockoutEnabled") VALUES ('2020-06-09 9:00:00', NULL, NULL, NULL, false, NULL, NULL, 0, false, true, 'TOBRADOS', '[email protected]', 'tobrados', 28, '[email protected]', 'Toni', 'Obrados', '', '666-66-66-66', true, true, false, true);

    The user looks at in Administrator/Users:

    (User is view in Administrator/Users)

    If I try to login with this user in admin session:

    (internal Server Error)

    If I login:

    (an internal error has occurred during your request)

    Regards.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @SASIMEXICO,

    Could you check server side log file (App_Data/Logs/Logs.txt) in your web project ? It should contain detailed error message.

  • User Avatar
    0
    SASIMEXICO created

    Hi @ismcagdas

    The log error is:

    DEBUG 2020-06-09 08:26:19,671 [148 ] GenTime.Web.Startup.Startup - Body: {"userNameOrEmailAddress":"tobrados","password":"to","rememberClient":false,"twoFactorRememberClientToken":null,"singleSignIn":false,"returnUrl":null} DEBUG 2020-06-09 08:26:19,671 [148 ] GenTime.Web.Startup.Startup - Host: api.gentime.dev.sasi.com.es DEBUG 2020-06-09 08:26:19,671 [148 ] GenTime.Web.Startup.Startup - Client IP: 80.174.193.89 INFO 2020-06-09 08:26:19,671 [148 ] pNetCore.Cors.Infrastructure.CorsService - CORS policy execution successful. INFO 2020-06-09 08:26:19,673 [148 ] tCore.Cors.Infrastructure.CorsMiddleware - No CORS policy found for the specified request. INFO 2020-06-09 08:26:19,674 [148 ] ft.AspNetCore.Routing.EndpointMiddleware - Executing endpoint 'GenTime.Web.Controllers.TokenAuthController.Authenticate (GenTime.Web.Core)' INFO 2020-06-09 08:26:19,683 [148 ] c.Infrastructure.ControllerActionInvoker - Route matched with {action = "Authenticate", controller = "TokenAuth", area = ""}. Executing controller action with signature System.Threading.Tasks.Task1[GenTime.Web.Models.TokenAuth.AuthenticateResultModel] Authenticate(GenTime.Web.Models.TokenAuth.AuthenticateModel) on controller GenTime.Web.Controllers.TokenAuthController (GenTime.Web.Core). WARN 2020-06-09 08:26:19,738 [132 ] GenTime.Authorization.Users.UserManager - Invalid password for user 1636. WARN 2020-06-09 08:26:19,747 [132 ] GenTime.Authorization.Users.UserManager - GetSecurityStampAsync for user 1636 failed because stamp was null. ERROR 2020-06-09 08:26:19,968 [132 ] Mvc.ExceptionHandling.AbpExceptionFilter - User security stamp cannot be null. System.InvalidOperationException: User security stamp cannot be null. at Microsoft.AspNetCore.Identity.UserManager1.GetSecurityStampAsync(TUser user) at Abp.Domain.Uow.UnitOfWorkInterceptor.InternalInterceptAsynchronous[TResult](IInvocation invocation) at Microsoft.AspNetCore.Identity.UserManager1.ValidateUserAsync(TUser user) at Microsoft.AspNetCore.Identity.UserManager1.UpdateUserAsync(TUser user) at Abp.Domain.Uow.UnitOfWorkInterceptor.InternalInterceptAsynchronous[TResult](IInvocation invocation) at Microsoft.AspNetCore.Identity.UserManager1.AccessFailedAsync(TUser user) at Abp.Domain.Uow.UnitOfWorkInterceptor.InternalInterceptAsynchronous[TResult](IInvocation invocation) at Abp.Authorization.AbpLogInManager3.TryLockOutAsync(Nullable1 tenantId, Int64 userId) at Abp.Domain.Uow.UnitOfWorkInterceptor.InternalInterceptAsynchronous[TResult](IInvocation invocation) at Abp.Authorization.AbpLogInManager3.LoginAsyncInternal(String userNameOrEmailAddress, String plainPassword, String tenancyName, Boolean shouldLockout) at Abp.Domain.Uow.UnitOfWorkInterceptor.InternalInterceptAsynchronous[TResult](IInvocation invocation) at Abp.Authorization.AbpLogInManager3.LoginAsync(String userNameOrEmailAddress, String plainPassword, String tenancyName, Boolean shouldLockout) at Abp.Domain.Uow.UnitOfWorkInterceptor.InternalInterceptAsynchronous[TResult](IInvocation invocation) at GenTime.Web.Controllers.TokenAuthController.GetLoginResultAsync(String usernameOrEmailAddress, String password, String tenancyName) in C:\Users\mmartinez5\Documents\git_sasi\gentime.backend\src\GenTime.Web.Core\Controllers\TokenAuthController.cs:line 830 at GenTime.Web.Controllers.TokenAuthController.Authenticate(AuthenticateModel model) in C:\Users\mmartinez5\Documents\git_sasi\gentime.backend\src\GenTime.Web.Core\Controllers\TokenAuthController.cs:line 288 at lambda_method(Closure , Object ) at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult() at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask1 actionResultValueTask) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) INFO 2020-06-09 08:26:19,980 [132 ] .Mvc.Infrastructure.ObjectResultExecutor - Executing ObjectResult, writing value of type 'Abp.Web.Models.AjaxResponse'. INFO 2020-06-09 08:26:19,980 [132 ] c.Infrastructure.ControllerActionInvoker - Executed action GenTime.Web.Controllers.TokenAuthController.Authenticate (GenTime.Web.Core) in 296.8959ms INFO 2020-06-09 08:26:19,980 [132 ] ft.AspNetCore.Routing.EndpointMiddleware - Executed endpoint 'GenTime.Web.Controllers.TokenAuthController.Authenticate (GenTime.Web.Core)' INFO 2020-06-09 08:26:19,989 [132 ] Microsoft.AspNetCore.Hosting.Diagnostics - Request finished in 318.1893ms 500 application/json; charset=utf-8 INFO 2020-06-09 08:27:20,339 [35 ] Microsoft.AspNetCore.Hosting.Diagnostics - Request starting HTTP/1.1 OPTIONS http://api.gentime.dev.sasi.com.es/api/TokenAuth/Authenticate
    DEBUG 2020-06-09 08:27:20,340 [35 ] GenTime.Web.Startup.Startup - Header: {

    After seeing the log my question is:

    How to insert a correct encrypted password record. How to insert a correct encrypted security stamp record.

    Regards,

  • User Avatar
    0
    musa.demir created

    How to insert a correct encrypted password record.

    IPasswordHasher<User> _passwordHasher
    //...
    public void UpdatePassword(User user)
    {
        user.Password = _passwordHasher.HashPassword(user, "123456");
    }
    

    How to insert a correct encrypted security stamp record.

    UserManager _userManager;
    //...
    public void SetSecurityStamp(User user){
        await _userManager.UpdateSecurityStampAsync(user);
    }
    

    Can you please run that codes for your user, after that your problem should be solved.

  • User Avatar
    0
    SASIMEXICO created

    Hi @demirmusa

    Perfect, after that, the problem is solved. But I don't want to use the libraries or the Abp driver. I'm insert the record directly in postgresql database. It is possible to do the encryption of the password and the security stam without the libraries or the Abp controllers.

    Regards,

  • User Avatar
    0
    musa.demir created

    Hi @SASIMEXICO We dont have any solution for that, I guess implementing that feature will need more effort than using existing one. As an another solution, Is excel user import usable for you ?