Hi!
Can you give an example of a Register controller to create new user?
Thank you!
8 Answer(s)
-
0
I mean Reigster ACTION not controller
/Account/Register
-
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
Is the user logged in after create async?
-
0
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.
-
0
One more time, thank you for the fast reply!
-
0
What is "CheckErrors" method and "_unitOfWorkManager" ?
-
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.
-
0
Got it working!!
I realized what CheckErrors was, but i thought it might be something more specific and important.
Thanks for the reply!