public Result <ServiceResponse> SignUp(SignUpRequest request) { return(this.UseConnection("Default", connection => { request.CheckNotNull(); Check.NotNullOrWhiteSpace(request.Email, "email"); Check.NotNullOrEmpty(request.Password, "password"); UserRepository.ValidatePassword(request.Email, request.Password, true); Check.NotNullOrWhiteSpace(request.DisplayName, "displayName"); if (connection.Exists <UserRow>( UserRow.Fields.Username == request.Email | UserRow.Fields.Email == request.Email)) { throw new ValidationError("EmailInUse", Texts.Validation.CantFindUserWithEmail); } using (var uow = new UnitOfWork(connection)) { string salt = null; var hash = UserRepository.GenerateHash(request.Password, ref salt); var displayName = request.DisplayName.TrimToEmpty(); var email = request.Email; var username = request.Email; var fld = UserRow.Fields; var userId = (int)connection.InsertAndGetID(new UserRow { Username = username, Source = "sign", DisplayName = displayName, Email = email, PasswordHash = hash, PasswordSalt = salt, IsActive = 0, InsertDate = DateTime.Now, InsertUserId = 1, LastDirectoryUpdate = DateTime.Now }); byte[] bytes; using (var ms = new MemoryStream()) using (var bw = new BinaryWriter(ms)) { bw.Write(DateTime.UtcNow.AddHours(3).ToBinary()); bw.Write(userId); bw.Flush(); bytes = ms.ToArray(); } var token = Convert.ToBase64String(MachineKey.Protect(bytes, "Activate")); var externalUrl = Config.Get <EnvironmentSettings>().SiteExternalUrl ?? Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/"); var activateLink = UriHelper.Combine(externalUrl, "Account/Activate?t="); activateLink = activateLink + Uri.EscapeDataString(token); var emailModel = new ActivateEmailModel(); emailModel.Username = username; emailModel.DisplayName = displayName; emailModel.ActivateLink = activateLink; var emailSubject = Texts.Forms.Membership.SignUp.ActivateEmailSubject.ToString(); var emailBody = TemplateHelper.RenderTemplate( MVC.Views.Membership.Account.SignUp.AccountActivateEmail, emailModel); var message = new MailMessage(); message.To.Add(email); message.Subject = emailSubject; message.Body = emailBody; message.IsBodyHtml = true; var client = new SmtpClient(); if (client.DeliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory && string.IsNullOrEmpty(client.PickupDirectoryLocation)) { var pickupPath = Server.MapPath("~/App_Data"); pickupPath = Path.Combine(pickupPath, "Mail"); Directory.CreateDirectory(pickupPath); client.PickupDirectoryLocation = pickupPath; } uow.Commit(); UserRetrieveService.RemoveCachedUser(userId, username); client.Send(message); return new ServiceResponse(); } })); }
public Result<ServiceResponse> SignUp(SignUpRequest request) { return this.UseConnection("Default", connection => { request.CheckNotNull(); Check.NotNullOrWhiteSpace(request.Email, "email"); Check.NotNullOrEmpty(request.Password, "password"); UserRepository.ValidatePassword(request.Email, request.Password, true); Check.NotNullOrWhiteSpace(request.DisplayName, "displayName"); if (connection.Exists<UserRow>( UserRow.Fields.Username == request.Email | UserRow.Fields.Email == request.Email)) { throw new ValidationError("EmailInUse", Texts.Validation.CantFindUserWithEmail); } using (var uow = new UnitOfWork(connection)) { string salt = null; var hash = UserRepository.GenerateHash(request.Password, ref salt); var displayName = request.DisplayName.TrimToEmpty(); var email = request.Email; var username = request.Email; var fld = UserRow.Fields; var userId = (int)new SqlInsert(fld.TableName) .Set(fld.Username, username) .Set(fld.Source, "sign") .Set(fld.DisplayName, displayName) .Set(fld.Email, email) .Set(fld.PasswordHash, hash) .Set(fld.PasswordSalt, salt) .Set(fld.IsActive, 0) .Set(fld.InsertDate, DateTime.Now) .Set(fld.InsertUserId, 1) .Set(fld.LastDirectoryUpdate, DateTime.Now) .ExecuteAndGetID(connection); byte[] bytes; using (var ms = new MemoryStream()) using (var bw = new BinaryWriter(ms)) { bw.Write(DateTime.UtcNow.AddHours(3).ToBinary()); bw.Write(userId); bw.Flush(); bytes = ms.ToArray(); } var token = Convert.ToBase64String(MachineKey.Protect(bytes, "Activate")); var externalUrl = Config.Get<EnvironmentSettings>().SiteExternalUrl ?? Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/"); var activateLink = UriHelper.Combine(externalUrl, "Account/Activate?t="); activateLink = activateLink + Uri.EscapeDataString(token); var emailModel = new ActivateEmailModel(); emailModel.Username = username; emailModel.DisplayName = displayName; emailModel.ActivateLink = activateLink; var emailSubject = Texts.Forms.Membership.SignUp.ActivateEmailSubject.ToString(); var emailBody = TemplateHelper.RenderTemplate( MVC.Views.Membership.Account.SignUp.AccountActivateEmail, emailModel); var message = new MailMessage(); message.To.Add(email); message.Subject = emailSubject; message.Body = emailBody; message.IsBodyHtml = true; var client = new SmtpClient(); if (client.DeliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory && string.IsNullOrEmpty(client.PickupDirectoryLocation)) { var pickupPath = Server.MapPath("~/App_Data"); pickupPath = Path.Combine(pickupPath, "Mail"); Directory.CreateDirectory(pickupPath); client.PickupDirectoryLocation = pickupPath; } uow.Commit(); UserRetrieveService.RemoveCachedUser(userId, username); client.Send(message); return new ServiceResponse(); } }); }
public Result <ServiceResponse> SignUp(SignUpRequest request) { return(this.UseConnection("Default", connection => { request.CheckNotNull(); Check.NotNullOrWhiteSpace(request.Email, "email"); Check.NotNullOrEmpty(request.Password, "password"); UserRepository.ValidatePassword(request.Email, request.Password, true); Check.NotNullOrWhiteSpace(request.DisplayName, "displayName"); if (connection.Exists <UserRow>( UserRow.Fields.Username == request.Email | UserRow.Fields.Email == request.Email)) { throw new ValidationError("EmailInUse", Texts.Validation.EmailInUse); } using (var uow = new UnitOfWork(connection)) { string salt = null; var hash = UserRepository.GenerateHash(request.Password, ref salt); var displayName = request.DisplayName.TrimToEmpty(); var email = request.Email; var username = request.Email; var fld = UserRow.Fields; var userId = (int)connection.InsertAndGetID(new UserRow { Username = username, Source = "sign", DisplayName = displayName, Email = email, PasswordHash = hash, PasswordSalt = salt, IsActive = 0, InsertDate = DateTime.Now, InsertUserId = 1, LastDirectoryUpdate = DateTime.Now }); byte[] bytes; using (var ms = new MemoryStream()) using (var bw = new BinaryWriter(ms)) { bw.Write(DateTime.UtcNow.AddHours(3).ToBinary()); bw.Write(userId); bw.Flush(); bytes = ms.ToArray(); } var token = Convert.ToBase64String(HttpContext.RequestServices .GetDataProtector("Activate").Protect(bytes)); var externalUrl = Config.Get <EnvironmentSettings>().SiteExternalUrl ?? Request.GetBaseUri().ToString(); var activateLink = UriHelper.Combine(externalUrl, "Account/Activate?t="); activateLink = activateLink + Uri.EscapeDataString(token); var emailModel = new ActivateEmailModel(); emailModel.Username = username; emailModel.DisplayName = displayName; emailModel.ActivateLink = activateLink; var emailSubject = Texts.Forms.Membership.SignUp.ActivateEmailSubject.ToString(); var emailBody = TemplateHelper.RenderViewToString(HttpContext.RequestServices, MVC.Views.Membership.Account.SignUp.AccountActivateEmail, emailModel); Common.EmailHelper.Send(emailSubject, emailBody, email); uow.Commit(); UserRetrieveService.RemoveCachedUser(userId, username); return new ServiceResponse(); } })); }
public Result <ServiceResponse> SignUp(SignUpRequest request) { return(this.UseConnection("Default", connection => { request.CheckNotNull(); Check.NotNullOrWhiteSpace(request.Email, "email"); Check.NotNullOrEmpty(request.Password, "password"); UserRepository.ValidatePassword(request.Email, request.Password, true); Check.NotNullOrWhiteSpace(request.DisplayName, "displayName"); if (connection.Exists <UserRow>( UserRow.Fields.Username == request.Email | UserRow.Fields.Email == request.Email)) { throw new ValidationError("EmailInUse", Texts.Validation.EmailInUse); } using (var uow = new UnitOfWork(connection)) { string salt = null; var hash = UserRepository.GenerateHash(request.Password, ref salt); var displayName = request.DisplayName.TrimToEmpty(); var email = request.Email; var username = request.Email; var fld = UserRow.Fields; var userModel = new UserRow { Username = username, Source = "sign", DisplayName = displayName, Email = email, PasswordHash = hash, PasswordSalt = salt, IsActive = 0, InsertDate = DateTime.Now, InsertUserId = 1, LastDirectoryUpdate = DateTime.Now }; var userId = (int)connection.InsertAndGetID(userModel); userModel.UserId = userId; var tenant = new TenantRow { TenantName = request.TenantName, CurrencyId = 1, SubscriptionRequired = true }; tenant.TenantId = Int32.Parse(connection.InsertAndGetID(tenant).ToString()); var offer = connection.ById <OffersRow>(request.OfferId); //Insert First subscription directly after you know the TenantId var subscriptionId = (int)connection.InsertAndGetID(new SubscriptionsRow { // TODO Get local string Name = string.Format("{0} - {1}", offer.Name, request.TenantName), OfferId = offer.OfferId, TenantId = Int32.Parse(tenant.TenantId.ToString()), SubscriptionEndDate = DateTime.Now.AddMonths(12), Enabled = 1, IsActive = 1, InsertUserId = userId, InsertDate = DateTime.Now, ActivatedOn = DateTime.Now, FreeDaysFromOffer = offer.MaximumSubscriptionTime ?? 0 }); tenant.SubscriptionId = subscriptionId; //Update Tenant SubscriptionId .. Is it Needet? connection.UpdateById(tenant, ExpectedRows.One); var userRoleId = (int)connection.InsertAndGetID(new UserRoleRow { UserId = userId, RoleId = offer.RoleId }); userModel.TenantId = tenant.TenantId ?? 2; connection.UpdateById(userModel, ExpectedRows.One); byte[] bytes; using (var ms = new MemoryStream()) using (var bw = new BinaryWriter(ms)) { bw.Write(DateTime.UtcNow.AddHours(3).ToBinary()); bw.Write(userId); bw.Flush(); bytes = ms.ToArray(); } var token = Convert.ToBase64String(HttpContext.RequestServices .GetDataProtector("Activate").Protect(bytes)); var externalUrl = Config.Get <EnvironmentSettings>().SiteExternalUrl ?? Request.GetBaseUri().ToString(); var activateLink = UriHelper.Combine(externalUrl, "Account/Activate?t="); activateLink = activateLink + Uri.EscapeDataString(token); var emailModel = new ActivateEmailModel(); emailModel.Username = username; emailModel.DisplayName = displayName; emailModel.ActivateLink = activateLink; var emailSubject = Texts.Forms.Membership.SignUp.ActivateEmailSubject.ToString(); var emailBody = TemplateHelper.RenderViewToString(HttpContext.RequestServices, MVC.Views.Membership.Account.SignUp.AccountActivateEmail, emailModel); Common.EmailHelper.Send(emailSubject, emailBody, email); uow.Commit(); UserRetrieveService.RemoveCachedUser(userId, username); return new ServiceResponse(); } })); }