private string GenerateToken(LevisUser user) { var parts = new List <byte[]> { BitConverter.GetBytes(DateTime.UtcNow.ToBinary()), (_providerUserKey).ToByteArray(), _privateTokenKey.ToByteArray(), Encoding.UTF8.GetBytes(user.UserName) }; var tokenBytes = new byte[parts.Sum(s => s.Length)]; var offset = 0; foreach (var part in parts) { Buffer.BlockCopy(part, 0, tokenBytes, offset, part.Length); offset += part.Length; } return(Base64UrlTextEncoder.Encode(tokenBytes.ToArray())); }
protected async override Task HandleCommandAsync(AddOrganisationCommand command) { var organisation = _mapper.Map <AddOrganisationCommand, Livis.Market.Data.Organisation>(command); _context.Organisations.Add(organisation); var username = organisation.Email; var password = GeneratePassword(); var user = new LevisUser() { Email = username, EmailConfirmed = true, IsPowerUser = false, NormalizedEmail = username, NormalizedUserName = username, PhoneNumber = organisation.PhoneNumber, PhoneNumberConfirmed = true, UserName = username }; var chkUser = await _userManager.CreateAsync(user, password).ConfigureAwait(false); if (chkUser.Succeeded) { var result = await _userManager.AddToRoleAsync(user, ComponentsHelper.Agency); var token = GenerateToken(user); command.ConfirmationLink = command.ConfirmationLink + "?token=" + token; await _notification.AgencyConfirmationAsync(command.ConfirmationLink, username, password); organisation.IsActivateAccount = false; organisation.IsSendConfirmationMail = true; organisation.IsSendThankYouMail = false; organisation.TokenConfirmationMail = token; _context.SaveChanges(); command.OrganisationId = organisation.OrganisationId; } else { throw new UnableCreateUserException("Please contact Administrator. System is unable creating organisation."); } }
public async static Task Initialize(LivisMarketContext context, UserManager <LevisUser> userManager, RoleManager <IdentityRole> roleManager) { context.Database.EnsureCreated(); context.Database.Migrate(); var isExist = await roleManager.RoleExistsAsync("Admin"); if (!isExist) { var role = new IdentityRole(); role.Name = "Admin"; await roleManager.CreateAsync(role); } isExist = await roleManager.RoleExistsAsync("Agency"); if (!isExist) { var role = new IdentityRole(); role.Name = "Agency"; await roleManager.CreateAsync(role); } isExist = await roleManager.RoleExistsAsync("Supplier"); if (!isExist) { var role = new IdentityRole(); role.Name = "Supplier"; await roleManager.CreateAsync(role); } isExist = await roleManager.RoleExistsAsync("Customer"); if (!isExist) { var role = new IdentityRole(); role.Name = "Customer"; await roleManager.CreateAsync(role); } isExist = context.Users.Any(u => u.UserName.Equals("Administrator", StringComparison.OrdinalIgnoreCase)); if (!isExist) { var user = new LevisUser(); user.UserName = "******"; user.Email = "*****@*****.**"; user.PhoneNumber = "01656154722"; user.PhoneNumberConfirmed = true; user.IsPowerUser = true; user.EmailConfirmed = true; string userPWD = "LivisMarket@2018"; IdentityResult chkUser = await userManager.CreateAsync(user, userPWD); if (chkUser.Succeeded) { var result = await userManager.AddToRoleAsync(user, "Admin"); } } isExist = context.Users.Any(u => u.UserName.Equals("Admin", StringComparison.OrdinalIgnoreCase)); if (!isExist) { var user = new LevisUser(); user.UserName = "******"; user.Email = "*****@*****.**"; user.IsPowerUser = true; user.EmailConfirmed = true; string userPWD = "LivisMarket@2018"; user.PhoneNumber = "00491622792803"; user.PhoneNumberConfirmed = true; IdentityResult chkUser = await userManager.CreateAsync(user, userPWD); if (chkUser.Succeeded) { var result = await userManager.AddToRoleAsync(user, "Admin"); } } }
public async Task ChangePassword(string newPassword, LevisUser currentUser) { currentUser.PasswordHash = _userManager.PasswordHasher.HashPassword(currentUser, newPassword); await _userManager.UpdateAsync(currentUser); }
protected override async Task HandleCommandAsync(RegisterUserCommand command) { try { var user = new LevisUser() { Email = command.Email, EmailConfirmed = true, IsPowerUser = false, NormalizedEmail = command.Email, NormalizedUserName = command.Email, PhoneNumber = command.PhoneNumber, PhoneNumberConfirmed = true, UserName = command.Email }; var chkUser = await _userManager.CreateAsync(user, command.Password).ConfigureAwait(false); if (chkUser.Succeeded) { var result = await _userManager.AddToRoleAsync(user, ComponentsHelper.Customer); await _notification.ThankYouAgencyConfirmationAsync(command.Email); var primaryContact = new CustomerContact(); primaryContact.Created = DateTime.Now; primaryContact.CreatorId = user.Id; primaryContact.Modified = DateTime.Now; primaryContact.ModifierId = user.Id; primaryContact.CustomerGroup = CustomerGroup.Customer; primaryContact.Email = command.Email; primaryContact.FirstName = command.FirstName; primaryContact.LastName = command.LastName; primaryContact.FullName = $"{command.LastName} {command.FirstName}"; primaryContact.Organisation = null; primaryContact.OrganisationId = null; primaryContact.OwnerId = user.Id; primaryContact.User = user; primaryContact.PhoneNumber = command.PhoneNumber; primaryContact.PreferredLanguage = _appSettings.DefaultLanguage; primaryContact.PreferredCurrency = _appSettings.DefaultCurrency; primaryContact.BillingAddress = new CustomerAddress() { Created = DateTime.Now, CreatorId = user.Id, }; primaryContact.BillingAddress.AddressType = CustomerAddressTypeEnum.Billing; primaryContact.BillingAddress.CityOrTownOrVillage = command.City; primaryContact.BillingAddress.Email = command.Email; primaryContact.BillingAddress.FirstName = command.FirstName; primaryContact.BillingAddress.LastName = command.LastName; primaryContact.BillingAddress.Modified = DateTime.Now; primaryContact.BillingAddress.ModifierId = user.Id; primaryContact.BillingAddress.Name = primaryContact.FullName; primaryContact.BillingAddress.PostCode = command.PostCode; primaryContact.BillingAddress.StreetAndHouseNumber = command.Street; primaryContact.BillingAddress.Prefecture = command.Prefecture; primaryContact.BillingAddress.PhoneNumber = command.PhoneNumber; _context.Contacts.Add(primaryContact); _context.SaveChanges(); } else { throw new RegisterUserException("Please contact Administrator. System is unable creating user."); } } catch (Exception ex) { throw new RegisterUserException(ex.Message, ex); } }