Base solution for your next web application
Open Closed

Register new user using userManager #189


User avatar
0
thiago floriano created

Hi!

Can you give an example of a Register controller to create new user?

Thank you!


8 Answer(s)
  • User Avatar
    0
    thiago floriano created

    I mean Reigster ACTION not controller

    /Account/Register

  • User Avatar
    0
    hikalkan created
    Support Team

    Actually, it's completely upon your needs. But I can share important parts of my Register method used in ASP.NET Iteration Zero (<a class="postlink" href="http://www.aspnetzero.com">www.aspnetzero.com</a>):

    [HttpPost]
    [UnitOfWork]
    public virtual async Task<ActionResult> Register(RegisterViewModel model)
    {
        //...
    
        if (!_multiTenancyConfig.IsEnabled)
        {
            model.TenancyName = Tenant.DefaultTenantName;
        }
        else if (model.TenancyName.IsNullOrEmpty())
        {
            throw new UserFriendlyException(L("TenantNameCanNotBeEmpty"));
        }
    
        var tenant = await GetActiveTenantAsync(model.TenancyName);
    
        var user = new User
        {
            TenantId = tenant.Id,
            Name = model.Name,
            Surname = model.Surname,
            UserName = model.UserName,
            EmailAddress = model.EmailAddress,
            Password = new PasswordHasher().HashPassword(model.Password),
            IsActive = true
        };
    
        _unitOfWorkManager.Current.EnableFilter(AbpDataFilters.MayHaveTenant);
        _unitOfWorkManager.Current.SetFilterParameter(AbpDataFilters.MayHaveTenant, AbpDataFilters.Parameters.TenantId, tenant.Id);
    
        user.Roles = new List<UserRole>();
        foreach (var defaultRole in await _roleManager.Roles.Where(r => r.IsDefault).ToListAsync())
        {
            user.Roles.Add(new UserRole { RoleId = defaultRole.Id });
        }
    
        CheckErrors(await _userManager.CreateAsync(user));
        await _unitOfWorkManager.Current.SaveChangesAsync();
    
        //...
    }
    

    This code works multi or single tenant. Notice that I'm setting filter value before using UserManager. I also added default roles to new user and saved it.

  • User Avatar
    0
    thiago floriano created

    Is the user logged in after create async?

  • User Avatar
    0
    hikalkan created
    Support Team

    No, because we may create a user as admin, so new user should not be logged in. You can user usermanager's login method after registering if you want.

  • User Avatar
    0
    thiago floriano created

    One more time, thank you for the fast reply!

  • User Avatar
    0
    thiago floriano created

    What is "CheckErrors" method and "_unitOfWorkManager" ?

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Since this is a commerical product, I can not share all codes here :) Have you tried coding it? If so, I'm sure you can find your answers. _unitOfWorkManager is simply injected IUnitOfWorkManager object. For CheckErrors, please see return value of CreateAsync, you will easily understand what it does.

  • User Avatar
    0
    thiago floriano created

    Got it working!!

    I realized what CheckErrors was, but i thought it might be something more specific and important.

    Thanks for the reply!