0
thiago floriano created
Hi!
Can you give an example of a Register controller to create new user?
Thank you!
8 Answer(s)
-
0
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.
-
0
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.
Have an answer to this question?
Log in
and write your answer.